![](/img/trans.png)
[英]Python + Selenium - How to check an image which is styled with CSS and displayed as content?
[英]How to check if an element is not displayed in selenium python
我如何检查是否未显示元素。 我认为它看起来像这样。
if(element.is_not_displayed):
doSomething()
else
doSomethingElse()
首先, Selenium中没有is_not_displayed
属性。
要模拟类似的逻辑,您可以使用try-except{}
循环来代替if-else
循环,从而为invisibility_of_element()
引入WebDriverWait ,并且可以使用以下Locator Strategy :
try:
WebDriverWait(driver, 30).until(EC.invisibility_of_element(element))
doSomething()
except TimeoutException:
doSomethingElse()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
您必须使用until_not
如下所示
WebDriverWait(driver, "time you want to wait".until_not(EC.presence_of_element_located((By.ID,"someID")))
例子:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"transpdiv-0"))) #here yout wait to the element appear
WebDriverWait(driver, 300).until_not(EC.presence_of_element_located((By.ID,"transpdiv-0"))) #here you wait the element disappear
注意:您必须添加相同的导入,如 undetect Selenium 说:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.