Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/disable javascript using Selenium WebDriver

For some reason, I've to disable javascript for Firefox (Manually, we do by following steps mentioned http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript). How can this be achieved by Selenium WebDriver using Ruby?

like image 444
Alpha Avatar asked Jun 21 '13 17:06

Alpha


People also ask

Can I use a disable JavaScript in the browser?

To enable or disable JavaScript on an Android mobile device, you need to access the browser settings. As there are many different browsers on Android devices, you may need to use some intuition to access the settings.

Can we automate JavaScript using selenium?

Selenium is an open-source automation testing tool that supports a number of scripting languages like C#, Java, Perl, Ruby, JavaScript, etc. Depending on the application to be tested, one can choose the script accordingly.


1 Answers

Yes, It is possible. But a different way. You first need to look into the link

  • Selenium::WebDriver::Firefox::Profile #[]=(key, value).

  • JavaScript settings

Once you would visit the link,try the below code :

require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile["javascript.enabled"] = false

driver = Selenium::WebDriver.for(:firefox, :profile => profile)

profile
# => #<Selenium::WebDriver::Firefox::Profile:0x89c7568
#     @additional_prefs=
#      {"javascript.enabled"=>false, "webdriver_firefox_port"=>7055},
#     @extensions=
#      {:webdriver=>
#        #<Selenium::WebDriver::Firefox::Extension:0x89c6488 # !> previous definition of proxy= was here
#         @path=
#          "/home/kirti/.rvm/gems/ruby-2.0.0-p0/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/extension/webdriver.xpi",
#         @should_reap_root=true>},
#     @load_no_focus_lib=false,
#     @model=nil,
#     @native_events=false,
#     @secure_ssl=false,
#     @untrusted_issuer=true>

Once your browser window will be opened up through the above code,then check the Preferences from Edit->Preferences->content,then you would see that Enable JavaScript: option is unchecked.

Enable JavaScript:

like image 106
Arup Rakshit Avatar answered Oct 02 '22 09:10

Arup Rakshit