如何知道元素是否出现然后消失在 selenium python ' is_displayed() 问题 '
How to know if element appear and then disappear in selenium python ' is_displayed() issues '
在我尝试自动化的网站中,当互联网连接丢失时,您可以通过 class 名称访问它的通知出现在页面顶部并告诉您没有互联网,我想编写一些代码,让您暂停该过程,直到互联网恢复
我试图通过 .is_displayed()
重新确认 return 在检查通知是否出现并且它工作正常之后的 True 值但是当我试图检查元素的消失时它显示了一个错误
except:
#time.sleep(3)
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0 :
print ('internet lost')
while True:
c= driver.find_element(By.CLASS_NAME, "HGcYc4pY").is_displayed()
if driver.find_element(By.CLASS_NAME, "KhLQZTRq pxYtrw1j D56bmevE").is_displayed() == True:
print("still lost")
if driver.find_element(By.CLASS_NAME, "KhLQZTRq pxYtrw1j D56bmevE").is_displayed() == False:
print("interent is back")
break
print("did it ")
我试过了,还是一样,当通知再次消失时它不起作用
except:
#time.sleep(3)
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0 :
print ('internet lost')
while True:
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0:
print("still lost")
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] == 0:
print("interent is back")
break
print("did it ")
我无法重现。我的意思是,我断开了我的电脑与互联网的连接并获得了“无互联网”页面,但我看不到带有 class 名称 HGcYc4pY
.
的元素
无论如何,我在这里可以建议的是:您可以使用 find_elements
方法代替 find_element
方法。它总是 return 您找到与传递的定位器匹配的元素列表。如果有匹配项,列表将为 non-empty,否则列表将为空。 Python 将空列表解释为布尔值 False
而 non-empty 列表被解释为布尔值 True
.
如果没有找到元素,这种方法永远不会抛出异常。
所以,你的逻辑可能是这样的:
if driver.find_elements(By.CLASS_NAME, "HGcYc4pY"):
#do whatever you need for the case the element is presented on the page
else:
#do whatever you want for the no element presented case
在我尝试自动化的网站中,当互联网连接丢失时,您可以通过 class 名称访问它的通知出现在页面顶部并告诉您没有互联网,我想编写一些代码,让您暂停该过程,直到互联网恢复
我试图通过 .is_displayed()
重新确认 return 在检查通知是否出现并且它工作正常之后的 True 值但是当我试图检查元素的消失时它显示了一个错误
except:
#time.sleep(3)
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0 :
print ('internet lost')
while True:
c= driver.find_element(By.CLASS_NAME, "HGcYc4pY").is_displayed()
if driver.find_element(By.CLASS_NAME, "KhLQZTRq pxYtrw1j D56bmevE").is_displayed() == True:
print("still lost")
if driver.find_element(By.CLASS_NAME, "KhLQZTRq pxYtrw1j D56bmevE").is_displayed() == False:
print("interent is back")
break
print("did it ")
我试过了,还是一样,当通知再次消失时它不起作用
except:
#time.sleep(3)
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0 :
print ('internet lost')
while True:
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] != 0:
print("still lost")
if driver.find_element(By.CLASS_NAME, "HGcYc4pY").size['width'] == 0:
print("interent is back")
break
print("did it ")
我无法重现。我的意思是,我断开了我的电脑与互联网的连接并获得了“无互联网”页面,但我看不到带有 class 名称 HGcYc4pY
.
的元素
无论如何,我在这里可以建议的是:您可以使用 find_elements
方法代替 find_element
方法。它总是 return 您找到与传递的定位器匹配的元素列表。如果有匹配项,列表将为 non-empty,否则列表将为空。 Python 将空列表解释为布尔值 False
而 non-empty 列表被解释为布尔值 True
.
如果没有找到元素,这种方法永远不会抛出异常。
所以,你的逻辑可能是这样的:
if driver.find_elements(By.CLASS_NAME, "HGcYc4pY"):
#do whatever you need for the case the element is presented on the page
else:
#do whatever you want for the no element presented case