如何使用Selify和Python等待元素包含属性style="Display:None;"如何使用、属性、元素、Python

由网友(拆開的心被折磨的半死不活)分享简介:使用Selify/Python时,我需要等待/暂停,直到显示:style="display:none;"单击按钮后,将显示以下内容(正在加载..。)
<div id="notification"..</div>

单击按钮后,将显示以下内容(正在加载..。)

怎么在cmd窗口执行py文件啊 python ,急急急 在线等,大神们帮下忙啊

<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>

然后,在将数据集加载到网页中后,加载...将消失(通过更改display:none),并出现以下情况:

<div id="notification" class="notification_info" style="display: none;">Loading...</div>

如何完成此操作(检查或等待style="display: none;"的值)?

因为style=display的页面中有很多<divs>,所以我需要同时等待dividstyle display

推荐答案

单击所需按钮后,文本为Loading...的元素将变为可见。因此,您可以看到HTML DOM中的元素为:

<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>

加载完成后,通过将style属性的display属性更改为:

,将文本为Loading...的元素变为不可见
style="display: none;"

因此WebElement在DOM Tree中表示为:

<div id="notification" class="notification_info" style="display: none;">Loading...</div>

解决方案

使用Selenium等待文本为Loading...的元素变为style="display: none;",您需要为invisibility_of_element()诱导WebDriverWait,您可以使用以下Locator Strategies之一:

使用CSS_SELECTOR

WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.notification_info#notification")))

使用XPATH

WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='notification_info' and @id='notification']")))

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

参考

您可以在以下位置找到相关的详细讨论:

How to click on anchor element with selenium using python? Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working