简体   繁体   中英

How to check whether an element on the DOM is in the foreground in Selenium using Java?

Is there a way to put a check whether the element is in the foreground?

截图

When I enter values in the pop-up window and click on "Save and Continue", I have put a check to see if the "Cancel" button( Highlighted in the image above ) is enabled and also tried another check to see if it's displayed.

driver.findElement(By.xpath("//input[contains(@id,'Cancel')]")).isEnabled(); 
driver.findElement(By.xpath("//input[contains(@id,'Cancel')]")).isDisplayed();

There's some loading time that the system takes for this pop up to go away and to bring back the focus on the main window.

But even before the pop-up can go away, the check( WebDriver wait ) I put to see if the "Cancel" button is enabled/is displayed, both return true and proceeds with the further execution.

And it works absolutely fine if I put in Thread.sleep for 2 seconds. But I don't want to stall the execution unconditionally. So now I need to find out if there's any way to check that an element is actually in the foreground and is in focus.

Could anyone help with this?

You need to add a wait for the disappearance of the popup. The code should look like this:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("XPath of popup")));

Hope it helps you!

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