[英]TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
I use this line code to wait until product_id appear, My code works well for about 30 minutes我使用此行代码等待 product_id 出现,我的代码运行良好约 30 分钟
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, product_id)))
And then, I get an error然后,我收到一个错误
TimeoutException(message, screen, stacktrace)selenium.common.exceptions.TimeoutException: Message:
I tried to change WebDriverWait to 100, but I still can't solve them.我尝试将 WebDriverWait 更改为 100,但我仍然无法解决它们。 I want to understand why to appear this error and any solution for this case.
我想了解为什么会出现此错误以及针对这种情况的任何解决方案。 Thanks so much !!!
非常感谢 !!!
This is my solution, but I want to know the cause of this error and looking for a better solution这是我的解决方案,但我想知道此错误的原因并寻找更好的解决方案
while True:
driver.get(URL)
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, product_id)))
break
except TimeoutException:
driver.quit()
Can you provide HTML example?你能提供HTML示例吗? Without HTML example it's nearly impossible to debug for you.
没有 HTML 示例,几乎不可能为您调试。 Code & HTML example will be helpful.
代码和 HTML 示例会有所帮助。
My guess is that your website uses dynamic id locators, or using iframe, or in your script, selenium is active on other tab/window/iframe/content.我的猜测是您的网站使用动态 id 定位器,或使用 iframe,或者在您的脚本中,硒在其他选项卡/窗口/iframe/内容上处于活动状态。 This is assuming your
product_id
is correct.这是假设您的
product_id
是正确的。
Update with the example:更新示例:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
PRODUCT_ID = 'productTitle'
driver = webdriver.Chrome()
driver.get("https://www.amazon.com/dp/B08BB9RWXD")
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, PRODUCT_ID)))
print(element.text)
except TimeoutException:
print("Cannot find product title.")
driver.quit()
I don't have any problem using above code.使用上面的代码我没有任何问题。 You can extend it by youself :).
你可以自己扩展它:)。
One thing that you need to note is that you DO NOT use while
& break
.您需要注意的一件事是不要使用
while
& break
。 WebDriverWait(driver, 10)
already does the loop to find your element for 10 seconds. WebDriverWait(driver, 10)
已经循环查找元素 10 秒。 In this 10 seconds, if it finds your element, it will stop finding ( break
).在这 10 秒内,如果它找到您的元素,它将停止查找(
break
)。 You don't need to do the while
loop yourself.您不需要自己执行
while
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.