繁体   English   中英

在selenium webdriver中停止无限页面加载 - python

[英]Stop infinite page load in selenium webdriver - python

我正在使用selenium web driver加载页面。但页面无限加载。 我试图抓住异常并模拟esc键操作,但这没有帮助。由于一些限制,我只能使用Firefox [我已经看到chrome添加解决方案]。 一旦我点击页面,我就无法获得控制权。

我将我的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')

我在你的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 docs): http//selenium-python.readthedocs.org/en/latest/faq.html#how-to-auto-save-files-using-custom-firefox-profile (向下滚动一点点直到你看到“这是一个例子:”下的代码块

让我知道它是怎么回事,祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM