Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable java script in Chrome Driver Selenium Python

How can I disable Java Script in Selenium's Chrome Driver using python

like image 556
Le Truong Sinh Avatar asked Jul 11 '16 08:07

Le Truong Sinh


People also ask

How to disable JavaScript in Chrome using Selenium?

New Selenium IDE Firstly, we have to create an object of the Options class. Then apply the set_preference method on that object. For disabling the JavaScript, we shall set the browser parameter javascript. enabled to False.

How do I disable Chrome Webdriver?

We can stop a page loading with Selenium webdriver in Chrome browser by using the JavaScript Executor. Selenium can execute JavaScript commands with the help of the executeScript command. To stop a page loading, the command window. stop() is passed as a parameter to the executeScript method.


2 Answers

Disabling JavaScript in Chrome is possible with old ChromeDriver prior to ChromeDriver2, which only supports Chrome 28 or under. try as below :-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-javascript")
driver = webdriver.Chrome(chrome_options=chrome_options)

WARNING: Running without JavaScript is unsupported and will likely break a large portion of the ChromeDriver's functionality. I suspect you will be able to do little more than navigate to a page. This is NOT a supported use case, and we will not be supporting it.

Hope it will help you...:)

like image 66
Saurabh Gaur Avatar answered Oct 12 '22 09:10

Saurabh Gaur


It's Really easy ! Just try this code !

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option( "prefs",{'profile.managed_default_content_settings.javascript': 2})
chrome = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
chrome.get('http://stackoverflow.com/')

If you want to disable Images, just replace javascript with image.

like image 39
Yassine Akermi Avatar answered Oct 12 '22 09:10

Yassine Akermi