简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM