Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Javascript after page has been rendered in Selenium Webdriver

I'm using Python. I've seen How can I disable javascript in firefox with selenium? which says to create a profile then load the driver with that profile. But I want to disable Javascript after a page has been rendered so this means I can't recreate the driver.

I've tried the following:

browser = selenium.webdriver.Firefox()
browser.get('http://wwwhatever.com')
browser.firefox_profile.set_preference('javascript.enabled', False)
browser.firefox_profile.update_preferences()

But I get this:

print browser.capabilities['javascriptEnabled']
True

Furthermore, the rest of my test still acts like Javascript is enabled.

like image 669
user1005909 Avatar asked Apr 19 '13 20:04

user1005909


1 Answers

It is possible with QuickJs extension - https://addons.mozilla.org/en-US/firefox/addon/quickjs/?src=search. This extension add Ctrl+q shortcut to disable javascript. Here is code:

profile = selenium.webdriver.FirefoxProfile()
profile.add_extension("addon-8753-latest.xpi") #QuickJs extension
browser = selenium.webdriver.Firefox(profile)
browser.get('http://wwwhatever.com')

#Turn of javascript, send Ctrl+q
body = browser.find_element_by_tag_name("body")
body.send_keys(selenium.webdriver.common.keys.Keys.CONTROL + 'q')
like image 113
Vihtinsky Avatar answered Sep 28 '22 07:09

Vihtinsky