简体   繁体   English

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

[英]How to open multiple windows using Selenium WebDriver

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.我试图使用 Selenium WebDriver 了解“在多个窗口之间切换”,但为此我无法使用 driver.get() 或 driver.navigate.to() 打开多个窗口,这会在同一窗口中打开链接。 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. n 的值是 1 而不是 2 作为它在同一窗口中的开口。 Please help.请帮忙。

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);

    }

}

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);

WebDriver navigate().to() and get() do exactly the same thing . WebDriver navigate().to()get()做完全一样的事情 There is no API to open a new window, but you can use ctrl+n hotkey:没有打开新窗口的 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 打开新窗口,关闭它,然后再次控制主窗口

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!希望这可以帮助!

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));
}

After call driver.get("http://www.google.com");调用driver.get("http://www.google.com"); , to open new window please follow the following approach. , 要打开新窗口,请遵循以下方法。

Use .sendKeys method:使用.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 OS please change Keys.CONTROL to Keys.COMMAND *注意:对于Mac OS请将Keys.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.希望这可以帮助。

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium webdriver 打开特定浏览器 - How to open specific browser using Selenium webdriver 如何使用Selenium WebDriver选择多个下拉菜单 - How to Select Multiple dropdowns using selenium webdriver 如何使用Selenium Webdriver在同一窗口中打开多个选项卡(超过2个)之间切换 - How to switch between multiple tabs(more than 2) open in same window using Selenium Webdriver 如何打开多个选项卡并通过Selenium和Webdriver在之间切换? - How to open multiple tabs and switch between through Selenium and Webdriver? 如何在 Selenium webdriver 中打开 chrome? - How to open chrome in Selenium webdriver? Selenium Webdriver:如何使用 Z8E00596AD8DE2213FF8F8D847CZ5D 从 Gmail 中删除打开的 email - Selenium Webdriver : How to delete an open email from Gmail using selenium webdriver 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何使用 JAVA 在 Selenium WebDriver 中打开 Chrome 开发者控制台 - How to open Chrome Developer console in Selenium WebDriver using JAVA 如何使用 Selenium WebDriver 打开受密码保护的 excel 文件 - How to open an excel file protected with password using Selenium WebDriver 如何使用 Selenium WebDriver 在新标签页(chrome)中打开链接? - How to open a link in new tab (chrome) using Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM