Stop endless page loading in selenium webdriver - python

I am loading a page using the selenium web driver. But the page loads endlessly. I tried to catch the exception and simulate the action of the esc key, but that did not help. With some limitations, I can only use Firefox [I saw how chrome adds a solution]. Once I got to the page, I did not get control.

I set my Firefox profile as

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) 

Script to stop downloading:

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

I see a few typos in your try / except block, so let's fix them very quickly ...

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

I have been working with Selenium and Python for some time now (I also use the Firefox web driver). Also, I assume that you are using Python, simply from the syntax of your code.

In any case, your Firefox profile should help solve the problem, but it doesn't seem like you are actually applying it to the driver instance.

Try something like this:

 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) 

This works for me using Python 2.7 and Selenium 2.46.

Source (Selenium docs): http://selenium-python.readthedocs.org/en/latest/faq.html#how-to-auto-save-files-using-custom-firefox-profile (scroll down until you see block of code in the section "Here is an example:")

Let me know how it goes, and good luck!

+8
source

All Articles