使用 Selenium/Python 时,我需要等待/暂停直到:style="display:none;"
显示为 <div id="notification"..</div>
单击按钮后,显示以下内容( Loading.. .)<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
的div
,以及 style display
.
最佳答案
单击所需按钮后,文本元素为 Loading...
变得可见。因此,您会看到 HTML DOM 中的元素作为:
<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>
加载完成后,文本为
Loading...
的元素通过更改 变得不可见display
的属性(property)style
属性为:style="display: none;"
因此 WebElement在 DOM Tree 中表示作为:
<div id="notification" class="notification_info" style="display: none;">Loading...</div>
解决方案
使用 Selenium等待文本为
Loading...
的元素转为style="display: none;"
你需要诱导WebDriverWait为 invisibility_of_element()
您可以使用以下任一 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
引用
您可以在以下位置找到一些相关的详细讨论:
关于python - 如何使用 Selenium 和 Python 等待元素包含属性样式 ="display:none;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62073863/