使用 Selenium/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;"
)?
因为有很多<divs>
在页面中style=display
,我需要等待id
of the div
,以及style display
.
单击所需的按钮后,该元素的文本为Loading...
变得可见。因此你会看到里面的元素HTML DOM as:
<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>
加载完成后,元素的文本为Loading...
通过改变变得不可见display
的财产style
属性为:
style="display: none;"
因此网页元素表示在DOM Tree as:
<div id="notification" class="notification_info" style="display: none;">Loading...</div>
Solution
Using Selenium等待文本为的元素Loading...
变成style="display: none;"
你需要诱导WebDriver等待为了invisibility_of_element()
您可以使用以下任一方法定位策略:
-
Using CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.notification_info#notification")))
-
Using XPATH
:
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='notification_info' and @id='notification']")))
-
Note:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考
您可以在以下位置找到一些相关的详细讨论:
- 如何使用python点击带有selenium的锚元素?
- Selenium invisibilityOf(element) 方法抛出 NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) 不起作用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)