Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 等待加载程序消失_Python_Selenium Webdriver - Fatal编程技术网

Python 等待加载程序消失

Python 等待加载程序消失,python,selenium-webdriver,Python,Selenium Webdriver,我也试过这个: 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_

我也试过这个:

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')]")))
我不知道如何检查,但可能我的元素总是出现在页面上,selenium认为它就在那里,唯一改变的是参数显示从无变为块。我想我可以得到像字符串这样的属性,并检查是否有单词“block”,但它是如此错误,我想。。。请帮帮我。

重申了您的答案(并进行了一些错误处理),以使人们更容易找到解决方案:)

导入所需的类:

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

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 

使用预期条件:元素不可见

这对我来说很好

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)的过度使用。这是一个天才的答案。多年来,我一直在为这个确切的想法奋斗,这是一个完美的解决方案。非常感谢。
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
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")))