How to open a new tab using Selenium WebDriver?

后端 未结 29 2463
野的像风
野的像风 2020-11-22 04:40

How to open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2)?

29条回答
  •  无人及你
    2020-11-22 05:24

    I had trouble opening a new tab in chrome for a while. Even driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); Didn't work for me.

    I found out that it's not enough that selenium has focus on driver, Windows also has to have the window in the front.

    My solution was to invoke an alert on chrome that would bring the window to front and then execute the command. sample code:

    ((JavascriptExecutor)driver).executeScript("alert('Test')");
    driver.switchTo().alert().accept();
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
    

提交回复
热议问题