How to open a new tab using Selenium WebDriver?

后端 未结 29 2839
野的像风
野的像风 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:14

    If you want to open the new tab you can use this

     ((JavascriptExecutor) getDriver()).executeScript("window.open()");
    

    If you want to open the link from the new tab you can use this

    with JavascriptExecutor:

     public void openFromNewTab(WebElement element){
                ((JavascriptExecutor)getDriver()).executeScript("window.open('"+element.getAttribute("href")+"','_blank');");
            }
    

    with Actions:

     WebElement element = driver.findElement(By.xpath("your_xpath"));
     Actions actions = new Actions(driver);
            actions.keyDown(Keys.LEFT_CONTROL)
                    .click(element)
                    .keyUp(Keys.LEFT_CONTROL)
                    .build()
                    .perform();
    

提交回复
热议问题