【问题标题】:Stop infinite page load in selenium webdriver - python在 selenium webdriver 中停止无限页面加载 - python
【发布时间】:2019-09-10 22:30:54
【问题描述】:

我正在使用 selenium 网络驱动程序加载页面。但是页面正在无限加载。 我试图捕捉异常并模拟 esc 键操作,但这没有帮助。由于某些限制,我只能使用 Firefox [我已经看到了 chrome add on 解决方案]。一旦我点击页面,我就无法恢复控制。

我将我的 Firefox 配置文件设置为

    firefoxProfile = FirefoxProfile()
    firefoxProfile.set_preference('permissions.default.stylesheet', 2)
    firefoxProfile.set_preference('permissions.default.image', 2)
    firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
    firefoxProfile.set_preference("http.response.timeout", 10)
    firefoxProfile.set_preference("dom.max_script_run_time", 10)

停止加载的脚本:

 try:
       driver.set_page_load_timeout(10)
       driver.get('http://www.example.com'     
 except Exception
        print 'time out'
        driver.send_keys(Keys.CONTROL +'Escape')

【问题讨论】:

  • 你能分享你使用的实际网址吗?
  • 这是我公司的项目,所以我不能公开分享网址。这会造成不必要的问题。
  • 好的,我明白了,您根本不会设置个人资料偏好?它仍然永远加载吗?另外,您是否尝试过等待 30 多秒?
  • 如果我没有设置任何 Firefox 配置文件,它会正确加载,但问题是页面有超过 100 个大尺寸图像。这使得加载过程非常慢。所以我想禁用图像.
  • 您好,感谢您的建议。然而,它并没有帮助停止页面加载。但是,如果我尝试应用 x-path 来提取异常块中的内容,而是试图停止页面加载,我可以获得我想要的内容,即使页面仍在加载。所以我正在应用 x-paths 来提取内容并关闭该窗口。

标签: python selenium-webdriver


【解决方案1】:

我在你的 try/except 块中看到了几个错别字,所以让我们尽快更正那些错别字...

try:
      driver.set_page_load_timeout(10)
      driver.get('http://www.example.com')
except Exception:
      print 'time out'
      driver.send_keys(Keys.CONTROL +'Escape')

我使用 Selenium 和 Python 已经有一段时间了(也使用 Firefox webdriver)。另外,我假设您使用的是 Python,只是从代码的语法来看。

无论如何,您的 Firefox 配置文件应该有助于解决问题,但看起来您实际上并未将其应用到 驱动程序 实例。

尝试以下方法:

from selenium import webdriver # import webdriver to create FirefoxProfile

firefoxProfile = webdriver.FirefoxProfile()
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
firefoxProfile.set_preference('permissions.default.image', 2)
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so','false')
firefoxProfile.set_preference("http.response.timeout", 10)
firefoxProfile.set_preference("dom.max_script_run_time", 10)

# now create browser instance and APPLY the FirefoxProfile
driver = webdriver.Firefox(firefox_profile=firefoxProfile)

这适用于我,使用 Python 2.7 和 Selenium 2.46。

来源(Selenium 文档):http://selenium-python.readthedocs.org/en/latest/faq.html#how-to-auto-save-files-using-custom-firefox-profile(向下滚动一点,直到您在“这是一个示例:”下看到代码块)

告诉我进展如何,祝你好运!

【讨论】:

  • 嗨,感谢您的建议。正如我之前在评论中提到的,通过在异常块上应用 xpath 已经解决了这个问题
  • 当然。我仍然建议添加一行代码 driver = webdriver.Firefox(firefox_profile=firefoxProfile) 以便实际使用您的 firefoxProfile 代码,因为现在,它不是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多