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

Jey*_*mar 11 python selenium-webdriver

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

要停止加载的脚本:

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

Bry*_*yce 9

我在你的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')
Run Code Online (Sandbox Code Playgroud)

我一直在使用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)
Run Code Online (Sandbox Code Playgroud)

这适用于我,使用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(向下滚动一点点直到你看到"这是一个例子:"下的代码块

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

  • 当然。我仍然建议添加一行代码 `driver = webdriver.Firefox(firefox_profile=firefoxProfile)` 以便实际使用您的 firefoxProfile 代码,因为现在它还没有 (2认同)