如何使用Java检查DOM上的元素是否在Selenium的前台?

时间:2022-08-17 07:20:07

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

有没有办法检查元素是否在前景?

如何使用Java检查DOM上的元素是否在Selenium的前台?

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.

但即使在弹出窗口消失之前,检查(WebDriver等待)我看看是否启用了“取消”按钮/,都返回true并继续执行。

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.

如果我将Thread.sleep放入2秒钟,它就可以正常工作。但我不想无条件地拖延执行。所以现在我需要找出是否有任何方法可以检查元素是否实际位于前景并且是焦点。

Could anyone help with this?

有人可以帮忙吗?

1 个解决方案

#1


2  

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!

希望它能帮到你!

#1


2  

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!

希望它能帮到你!