java 如何使用 Selenium WebDriver 打开多个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32786456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:46:13  来源:igfitidea点击:

How to open multiple windows using Selenium WebDriver

javaselenium

提问by Sam

I was trying to learn about "switching between multiple windows" using Selenium WebDriver but for that I am unable to OPEN multiple windows using driver.get() or driver.navigate.to(), that opens the links in the SAME window. Can someone help me to open multiple windows using the same driver instance? I have provided my sample code. That value of n is coming as 1 and not 2 as its opening in the same window. Please help.

我试图使用 Selenium WebDriver 了解“在多个窗口之间切换”,但为此我无法使用 driver.get() 或 driver.navigate.to() 打开多个窗口,这会在同一窗口中打开链接。有人可以帮助我使用同一个驱动程序实例打开多个窗口吗?我已经提供了我的示例代码。n 的值是 1 而不是 2 作为它在同一窗口中的开口。请帮忙。

public class MultipleWindows {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);


        driver.get("http://www.google.com");

        driver.navigate().to("http://www.facebook.com");


        int n = driver.getWindowHandles().size();
        System.out.println(n);

    }

}

回答by Madhan

This will also do

这也会做

This will create a new tab/window and open the given url

这将创建一个新选项卡/窗口并打开给定的 url

String url="whatever url or empty to open a empty tab";
((JavascriptExecutor) driver).executeScript("window.open(arguments[0])", url);

回答by Dmitry Baev

WebDriver navigate().to()and get()do exactly the same thing. There is no API to open a new window, but you can use ctrl+nhotkey:

WebDrivernavigate().to()get()做完全一样的事情。没有用于打开新窗口的 API,但您可以使用ctrl+n热键:

    WebDriver driver = new FirefoxDriver();
    WebElement body = driver.findElement(By.tagName("body"));
    body.sendKeys(Keys.chord(Keys.CONTROL, "n"));
    System.out.println(driver.getWindowHandles().size());
    driver.quit();

Or you can use few instances of WebDriver (see Selenium Java open new window, close it, and control main window again)

或者您可以使用 WebDriver 的几个实例(请参阅Selenium Java 打开新窗口,关闭它,然后再次控制主窗口

回答by Huy Hóm H?nh

One more way from there

那里开始的另一种方式

        WebDriver driver = new ChromeDriver();
        driver.get(adminToolURL);
        Set<String> windows = driver.getWindowHandles();
        String adminToolHandle = driver.getWindowHandle();
        ((JavascriptExecutor) driver).executeScript("window.open();");
        Set<String> customerWindow = driver.getWindowHandles();
        customerWindow.removeAll(windows);
        String customerSiteHandle = ((String) customerWindow.toArray()[0]);
        driver.switchTo().window(customerSiteHandle);
        driver.get(customerSiteURL);
        driver.switchTo().window(adminToolHandle);

Hope this helps!

希望这可以帮助!

回答by Krzysztof Walczewski

I used clear way. I created driverA and driverB, than i can open 2 windows and operate on its without any mistake:

我用了明确的方式。我创建了驱动程序 A 和驱动程序 B,然后我可以打开 2 个窗口并在没有任何错误的情况下对其进行操作:

private WebDriver driverA;
private WebDriver driverB;

@BeforeClass(alwaysRun = true)
private void executeBeforeClass() {
    driverB = BrowserFactory.startBrowser(browser, baseUrl);
    driverA = BrowserFactory.startBrowserAsIncognito(browser, baseUrl);
}

@AfterClass(alwaysRun = true)
public void runAfterClass(){
    if(driverA!=null){
        driverA.quit();
    }
    if(driverB!=null){
        driverB.quit();
    }
}



@Test
public void shouldEhcacheWorkFine() {

    new LoginPage(driverA).loginToTomcatA(login, password)
            .goToListCompaniesPage()
            .goToEditFirstCompanyPage()
            .setPersonName(NAME);

    EditPage editPageB = new LoginPage(driverB).loginToTomcatB(login, password)
            .goToListCompaniesPage()
            .goToEditFirstCompanyPage();
    assertThat(editCompanyPageB.getPersonName(), is(NAME));
}

回答by frianH

After call driver.get("http://www.google.com");, to open new window please follow the following approach.

调用后driver.get("http://www.google.com");,要打开新窗口,请按照以下方法操作。

Use .sendKeysmethod:

使用.sendKeys方法:

String multipleKeys = Keys.chord(Keys.CONTROL, "t");
driver.findElement(By.tagName("body")).sendKeys(multipleKeys);

Use Actions:

使用Actions

Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();

*Note: For Mac OSplease change Keys.CONTROLto Keys.COMMAND

*注Mac OSKeys.CONTROL改为Keys.COMMAND

Use JavascriptExecutor:

使用JavascriptExecutor

  • Open new blank window:
  • 打开新的空白窗口:
((JavascriptExecutor) driver).executeScript("window.open()");
  • Open new window with specific url:
  • 使用特定网址打开新窗口:
((JavascriptExecutor) driver).executeScript("window.open('https://google.com')");

Hope this helps.

希望这可以帮助。

回答by Vijay

for (int i = 1; i < 10; i++) {
            ((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com/')");
            ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
            driver.switchTo().window(tabs.get(i));
            Thread.sleep(2000);
        }