【问题标题】:Wait until loader disappears python selenium等到加载器消失 python selenium
【发布时间】:2014-11-23 01:58:49
【问题描述】:
<div id="loader-mid" style="position: absolute; top: 118.5px; left: 554px; display: none;">
    <div class="a">Loading</div>
    <div class="b">please wait...</div>
</div>

并想等到它消失。我有以下代码,但有时它等待的时间太长,并且在某些代码点它突然冻结所有进程,我不知道为什么。

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

self.wait = WebDriverWait(driver, 10)

self.wait.until(EC.invisibility_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]")))

我也试过这个:

self.wait.until_not(EC.presence_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]")))

我不知道如何检查,但也许我的元素总是出现在页面上并且 selenium 认为它在那里,唯一改变的是参数显示从无变为块。我想我可以得到像字符串这样的属性,并检查是否有“块”这个词,但它太不对劲了……请帮帮我。

【问题讨论】:

  • 您是否尝试过检查隐藏在加载器后面的元素的可见性?
  • 是的,但对我没有帮助。但不知何故,现在第二个变体起作用了。奇怪。
  • 是的,这很奇怪。

标签: python selenium-webdriver


【解决方案1】:

重申您的答案(带有一些错误处理),以便人们更容易找到解决方案:)

导入所需的类:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

配置变量:

SHORT_TIMEOUT  = 5   # give enough time for the loading element to appear
LONG_TIMEOUT = 30  # give enough time for loading to finish
LOADING_ELEMENT_XPATH = '//*[@id="xPath"]/xPath/To/The/Loading/Element'

代码解决方案:

try:
    # wait for loading element to appear
    # - required to prevent prematurely checking if element
    #   has disappeared, before it has had a chance to appear
    WebDriverWait(driver, SHORT_TIMEOUT
        ).until(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH)))

    # then wait for the element to disappear
    WebDriverWait(driver, LONG_TIMEOUT
        ).until_not(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH)))

except TimeoutException:
    # if timeout exception was raised - it may be safe to 
    # assume loading has finished, however this may not 
    # always be the case, use with caution, otherwise handle
    # appropriately.
    pass 

【讨论】:

  • 这是一个天才的答案。多年来,我一直在为这个确切的想法苦苦挣扎,这是完美的解决方案。谢谢
  • 我用它来等待模态对话框出现然后消失..完美工作!谢谢
【解决方案2】:

使用预期条件:invisibility_of_element_located

这对我来说很好。

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


WebDriverWait(driver, timeout).until(EC.invisibility_of_element_located((By.ID, "loader-mid")))

【讨论】:

    【解决方案3】:

    以下代码创建一个无限循环,直到元素消失:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    
    while True:
            try:
                WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, 'your_xpath')))
            except TimeoutException:
                break
    

    【讨论】:

    • 这个问题是元素可能没有时间先加载,所以它会立即跳出循环并继续。无限循环也很少是一件好事,除非有办法取消它们,并且它们通常应该在循环内进行某种睡眠以防止过度使用资源 (cpu)。
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2020-04-21
    • 2018-11-01
    • 1970-01-01
    • 2014-12-21
    相关资源
    最近更新 更多