gpt4 book ai didi

python - 等到加载器消失 python selenium

转载 作者:太空狗 更新时间:2023-10-29 20:21:31 24 4
gpt4 key购买 nike

<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')]")))

我不知 Prop 体如何检查,但也许我的元素总是出现在页面上并且 selenium 认为它在那里,唯一改变的是参数显示从无变为 block 。我想我可以得到像字符串这样的属性并检查是否有单词“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

关于python - 等到加载器消失 python selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26086965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com