2

我如何在 selenium webdriver ( firefox ) 的新选项卡上打开链接?

<a href='/test' id='test'>Link</a>

driver.find_element_by_id('test').click()
4

2 回答 2

1

看起来您目前最好的选择是将锚标记注入页面。您需要使其适应 python,但它应该相对简单:https ://stackoverflow.com/a/9122450/39843

于 2012-05-03T18:06:09.370 回答
1
package com.crm.qa.BaseTest;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NewTabChrome {

    public static void main(String[] args) throws AWTException {

        System.setProperty("webdriver.chrome.driver", 
                "C:/Users/sunil/Downloads/chromedriver_win32 (2)/chromedriver.exe");

        WebDriver driver = new ChromeDriver();//open browser
        driver.manage().window().maximize();//browser maximize
        driver.get("http://www.google.com");//open google 

        //open new tab 
        for(int i = 0; i<=1;i++){
        Robot rob = new Robot();
        rob.keyPress(KeyEvent.VK_CONTROL);
        rob.keyPress(KeyEvent.VK_T);
        rob.keyRelease(KeyEvent.VK_CONTROL);
        rob.keyRelease(KeyEvent.VK_T);
        ArrayList<String> tabs1 = new ArrayList<String> (driver.getWindowHandles());
        //Switch to new tab
        driver.switchTo().window((String) tabs1.get(i));
    }
        //open facebook
        driver.get("http://facebook.com");
        driver.quit();
    }
}
于 2019-04-19T09:35:17.157 回答