新的浏览器选项卡打开,在SELENIUM WEBDRIVER JAVA之后找不到WebELements

时间:2016-03-10 12:40:28

标签: java selenium selenium-webdriver

我正在运行以下方法:

public void AssertPreviousSubmissionClick() throws InterruptedException{
    EnterLoginCredentials();
    PreviousSubmissionClick();  //After this action takes place a new Browser Tab Opens
    Assert.assertTrue("Clicking the link didn't work!",oldLogo.isDisplayed());
    Reporter.log("PASSED! User Successfully click the Previous Year(s) Submission Request and was taken into the Grant Process!");
}

我在PreviousSubmissionClick()处留下了一个注释,此时打开了一个新的浏览器Tab。在此方法中,我无法再在打开的新浏览器选项卡上标识WebElements。请建议我如何在新的浏览器选项卡上找到WebElements。

2 个答案:

答案 0 :(得分:3)

您需要将焦点切换到新标签页

String currentTab = driver.getWindowHandle();
for (String tab : driver.getWindowHandles()) {
    if (!tab.equals(currentTab) {
        driver.switchTo().window(tab); 
    }       
}
// do something on new tab

关闭新标签并切换回来

driver.close();
driver.switchTo().window(currentTab);

答案 1 :(得分:0)

试试这个,它会起作用。

public void handle(){
WebDriver driver;
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-switch-windows/");
driver.findElement(By.xpath(".//*[@id='content']/p[4]/button")).click();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
System.out.println(driver.getTitle());
}