Do not want the Images to load and CSS to render on Firefox in Selenium WebDriver - Python

后端 未结 6 866
长情又很酷
长情又很酷 2020-11-29 00:37

I am using Selenium 2 with python bindings to fetch some data from our partner\'s site. But on an average it\'s taking me around 13 secs to perform this operation.

I

6条回答
  •  青春惊慌失措
    2020-11-29 00:59

    Unfortunately the option firefox_profile.set_preference('permissions.default.image', 2) no longer seems to work to disable images with the latest version of Firefox - [for reason see Alecxe's answer to my question Can't turn off images in Selenium / Firefox ]

    The best solution i had was to use the firefox extension quickjava , which amongst other things can disable images- https://addons.mozilla.org/en-us/firefox/addon/quickjava/

    My Python code:

     from selenium import webdriver
     firefox_profile = webdriver.FirefoxProfile()
    
     firefox_profile.add_extension(folder_xpi_file_saved_in + "\\quickjava-2.0.6-fx.xpi")
     firefox_profile.set_preference("thatoneguydotnet.QuickJava.curVersion", "2.0.6.1") ## Prevents loading the 'thank you for installing screen'
     firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2)  ## Turns images off
     firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.AnimatedImage", 2)  ## Turns animated images off
    
     driver = webdriver.Firefox(firefox_profile)
     driver.get(web_address_desired)
    

    Disabling CSS (and i think flash) still work with firefox propertiees. but they and other parts can also be switched off by adding the lines:

      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.CSS", 2)  ## CSS
      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Cookies", 2)  ## Cookies
      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Flash", 2)  ## Flash
      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Java", 2)  ## Java
      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.JavaScript", 2)  ## JavaScript
      firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2) 
    

提交回复
热议问题