Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop infinite page load in selenium webdriver - python

I am loading a page using selenium web driver.But the page is loading infinitely. I tried to catch the exception and simulate the esc key action but that didn't helped.Due to some constraints I can use only Firefox[I have seen the chrome add on solution]. Once I hit the page I am not getting the control back.

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 loading :

 try:
       driver.set_page_load_timeout(10)
       driver.get('http://www.example.com'     
 except Exception
        print 'time out'
        driver.send_keys(Keys.CONTROL +'Escape')
like image 855
Jeya Kumar Avatar asked Aug 07 '15 11:08

Jeya Kumar


1 Answers

I see a couple of typos in your try/except block, so let's correct those really 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 a while now (also using Firefox webdriver). Also, I'm assuming you're using Python, just from the syntax of your code.

Anyways, your Firefox profile should help resolve the issue, but it doesn't look like you're actually applying it to the driver instance.

Try something along these lines:

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 a tiny bit til you see code block under "Here is an example:")

Let me know how it goes, and good luck!

like image 110
Bryce Avatar answered Oct 25 '22 00:10

Bryce