【问题标题】:How to wait for an element to be contain the attribute style="display:none;" using Selenium and Python如何等待元素包含属性 style="display:none;"使用 Selenium 和 Python
【发布时间】:2020-09-16 07:25:09
【问题描述】:

使用 Selenium/Python 时,我需要等待/暂停直到:style="display:none;" 显示为 <div id="notification"..</div>


点击一个按钮后,显示如下(Loading...)

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

然后在网页中加载数据集后,Loading... 消失(通过更改 display:none),出现以下内容:

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


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

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

【问题讨论】:

  • 您能否提供代码并指定您卡在哪一点?
  • 我还没有任何代码可以解决这个问题,我被困在那里了。

标签: python selenium xpath css-selectors display


【解决方案1】:

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

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

加载完成后,通过将 style 属性的 display 属性更改为: 属性使带有 Loading... 文本的元素不可见: p>

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
    

参考

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2017-10-04
    • 2019-09-16
    • 2021-08-05
    相关资源
    最近更新 更多