由网友(拆開的心被折磨的半死不活)分享简介:使用Selify/Python时,我需要等待/暂停,直到显示:style="display:none;"单击按钮后,将显示以下内容(正在加载..。)
<div id="notification"..</div>
单击按钮后,将显示以下内容(正在加载..。)
<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>
,所以我需要同时等待div
的id
和style 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相关推荐
最新文章