How to disable Javascript when using Selenium?

前端 未结 16 724
星月不相逢
星月不相逢 2020-12-05 05:07

I am wondering how do I disable javascript when using selenium so I can test server side validation.

I found this article but I don\'t know what to really do. Like I

16条回答
  •  醉酒成梦
    2020-12-05 05:43

    As of 2018 the above solutions didn't work for me for Firefox 61.0.1 and geckodriver 0.20.1.

    And here is a solution which works.

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    browser_exe = '/path/to/firefox/exe'
    browser_driver_exe = '/path/to/geckodriver/exe'
    
    firefox_binary = FirefoxBinary(browser_exe)
    
    profile = webdriver.FirefoxProfile()
    profile.DEFAULT_PREFERENCES['frozen']['javascript.enabled'] = False
    profile.set_preference("app.update.auto", False)
    profile.set_preference("app.update.enabled", False)
    profile.update_preferences()
    
    driver = webdriver.Firefox(
        executable_path=browser_driver_exe,
        firefox_binary=firefox_binary,
        firefox_profile=profile,
    )
    
    driver.get("about:config")
    

    Now in the search bar type javascript and you should see the following.

提交回复
热议问题