簡體   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