python - 使用 Python 在 Selenium Webdriver (Selenium 2) 中显式等待和隐式等待的问题

标签 python selenium-webdriver

我必须使用一些非常慢的代理,并且时不时地卡住。因此,我正在尝试为此找到解决方案/解决方法,这是我的问题:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

browser = webdriver.Firefox()
browser.get("http://whateversite.com")

element = browser.find_element_by_id("element")
element.click() # go to page http://whateversite.com/page.html

new_element = browser.find_element_by_id("newElement")

无论您是预先设置browser.implicitly_wait(30)还是使用new_element = WebDriverWait(browser, 30).until(lambda browser : browser.find_element_by_id("newElement")) 它只是卡住了,有时会持续 HOURS。它似乎在等待该页面 http://whateversite.com/page.html 完全加载,在某些极端情况下可能需要数小时。

我应该怎么做才能避免这种情况?

最佳答案

尝试使用不稳定的负载策略。 例如FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("webdriver.load.strategy", "unstable"); WebDriver driver = new FirefoxDriver(profile); ` 您可以在 http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#current-document-readiness 找到更多信息。

也试试这个。 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#pageLoadTimeout%28long,%20java.util.concurrent.TimeUnit%29

关于python - 使用 Python 在 Selenium Webdriver (Selenium 2) 中显式等待和隐式等待的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10756609/

相关文章:

python - OpenCV 简单 Blob 检测器未检测到所有 Blob

python - 如何使用PIL(Python Image Library)旋转图片让黑色背景变透明

python - 使用 plotly python 导出 pdf

java - 使用 for each loop 和 Map 创建 JSON 文件 - 它在 for 循环的所有迭代中重复第一个元素值

Selenium EdgeDriver 禁用保存密码对话框

Python Selenium send_keys 函数发送部分文本

python - 如何在 LightGBM 中设置 max_bin 参数

python - Python 中的 Kruskal 算法

ruby - 在 selenium webdriver ruby​​ 中解析 Pdf 时出错

python - Selenium 单击到下一页直到最后一页