当前位置: 动力学知识库 > 问答 > 编程问答 >

New Browser Tab Opens, Can't find WebELements after SELENIUM WEBDRIVER JAVA

问题描述:

I am running the following method:

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

}

I left a note at PreviousSubmissionClick() at which point a new browser Tab is open. At this point in the method I can no longer identify WebElements on the new browser tab that opens. Please suggest how I can find WebElements on the new browser tab.

网友答案:

You need to switch the focus to the new tab

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

And to close the new tab and switch back

driver.close();
driver.switchTo().window(currentTab);
网友答案:

Try this ,it will work.

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());
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: