简体   繁体   中英

Find a WebElement on the new page using Selenium WebDriver with Java

I'm trying to automate a functionality where I have to open a new tab and start to work on this page, but my code still looking the WebElements on the last Tab. I try to switch the tabs with the follow code but its not working.

public void DataManager() throws InterruptedException {
    ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(0));
    driver.close();
    driver.switchTo().window(tabs2.get(1));
    WebElement tes = driver.findElement(By.xpath("//*[@id=\"lui-popover-3\"]/div/ng-transclude/ul/li[2]/button[1]/span[2]"));
    tes.click();                                
}   

What's going on, and how can I fix this?

1st time, You have focus driver on 1st tab which is on 0 index and you have close it. After you close it, Your 1 index become 0 index but you have invoke it on 1 index.

You may Refer sample example:

ArrayList<String> tabs= new ArrayList<String>(driver.getWindowHandles());
js.executeScript("window.open()");      
driver.switchTo().window(tabs.get(1));

Here, by this lines New tab will be open by keeping current Tab. And driver will invoke for Tab-1.

I found the solution, I had a problem to find Web Elements in my page, so when I used the following code to change the selenium focus:

ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));

And I searched the Web Element using press Tab I found it.

Thanks!

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