python - 如何使用 Selenium 和 Python 等待元素包含属性样式 ="display:none;"

标签 python selenium xpath css-selectors display

使用 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 的页面中,我需要等待iddiv ,以及 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;"

因此 WebElementDOM Tree 中表示作为:
<div id="notification" class="notification_info" style="display: none;">Loading...</div>

解决方案

使用 Selenium等待文本为 Loading... 的元素转为style="display: none;"你需要诱导WebDriverWaitinvisibility_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
    


  • 引用

    您可以在以下位置找到一些相关的详细讨论:
  • How to click on anchor element with selenium using python?
  • Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
  • 关于python - 如何使用 Selenium 和 Python 等待元素包含属性样式 ="display:none;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62073863/

    相关文章:

    python - 如何快速获取使用 PyCharm 和 Pytorch 的文档

    javascript - NightwatchJS : How to check if attribute is not present?

    python - 如何在另一个 "frame window"内的窗口中查找元素的 xpath

    c# - 当命名空间不再可用时解析 xml 文档

    python - 将文本转换为嵌套列表

    python - 具有可能不精确的纬度/经度坐标的 DBSCAN

    python - 在 Selenium 上选择其他标签内的 Iframe

    java - Selenium:不明白如何为 IMDB 热门电影编写正确的 xpath

    python - 在类中没有 'self' 参数的函数有什么意义?

    java - Selenium 中 try catch block 中带有 isDisplayed() 方法的 NoSuchElementException