繁体   English   中英

selenium python 中如何查看某个元素是否没有显示

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM