如何检查100%覆盖的WebElement是否可以与Selenium一起点击 [英] How to Check If 100% Covered WebElement is Clickable with Selenium

查看:249
本文介绍了如何检查100%覆盖的WebElement是否可以与Selenium一起点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个绝对位置的div

I have two divs with absolute position

<div id="4711" style="position:absolute;top:0px;bottom:0px;left:0px;right:0px;background-color:red">Visible later</div>
<div id="4712" style="position:absolute;top:0px;bottom:0px;left:0px;right:0px;background-color:green">To be removed</div>

和一些Javascript(此处未显示)一段时间后移除4712(比如说2秒后)来自DOM。

and some Javascript (not shown here) that removes 4712 after a while (lets say after 2 seconds) from the DOM.

现在,在我的Selenium测试中,我想检查4711是否可点击。
从用户的角度来看,只有在删除4712后才能点击它。

Now, in my Selenium tests I want to check if 4711 is clickable. From a user's perspective it is only clickable after 4712 has been removed.

所以我试过

new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.elementToBeClickable(By.id("4711")));

但是,4711总是可点击(启用=真,显示=真),即使在删除4712之前。

However, 4711 is always clickable (enabled=true, displayed=true), even before 4712 is removed.

有没有办法检查4711是否真的可以点击,也就是说,从用户的角度来看是否可点击(理想情况下不使用Javascript)?

Is there any way how to check if 4711 is realy clickable, that is, clickable from a user's perspective (ideally without using Javascript)?

推荐答案

正如您所提到的, Javascript 会删除元素 4712 如果元素变得陈旧变得不可见,有点不清楚。因此,对于此步骤,您可以使用以下任一选项:

As you mentioned, a Javascript removes the element 4712 it is a bit unclear if the element becomes stale or becomes invisible. So for this step you can use either of the following options:

new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.stalenessOf(driver.findElement(By.id("4712"))));


  • invisibilityOfElementLocated()

    new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.invisibilityOfElementLocated(By.id("4712")));
    


  • not 以及 visibilityOfElementLocated()

    new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("4712"))));
    


  • not 以及 presenceOfElementLocated()

    new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.id("4712"))));
    


  • 下一步要验证是否元素 4711 是否可点击,您可以使用以下代码行:

    For the next step you want to validate if element 4711 is clickable or not and you can use the following line of code:

    new WebDriverWait(browserInstance.getWebDriver(), 5).until(ExpectedConditions.elementToBeClickable(By.id("4711")));
    

    注意:元素的状态为 enabled = true displayed = true 不等于元素 可互动 可点击

    Note: An element's state as enabled=true and displayed=true isn't equivalent to element is interactable i.e. clickable

    这篇关于如何检查100%覆盖的WebElement是否可以与Selenium一起点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆