javascript - 在 Selenium Python 中禁用图像

标签 javascript python css selenium selenium-webdriver

因为 Webdriver 在继续下一行之前等待整个页面加载,我认为禁用图像、css 和 javascript 会加快速度。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

def disableImages(self):
    ## get the Firefox profile object
    firefoxProfile = FirefoxProfile()
    ## Disable CSS
    firefoxProfile.set_preference('permissions.default.stylesheet', 2)
    ## Disable images
    firefoxProfile.set_preference('permissions.default.image', 2)
    ## Disable Flash
    firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
                                      'false')
    ## Set the modified profile while creating the browser object 
    self.browserHandle = webdriver.Firefox(firefoxProfile)

我从 stackoverflow 得到代码 Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python

但是当我添加

driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com/")

到最后,它仍然加载图像:/

最佳答案

不幸的是,选项 firefox_profile.set_preference('permissions.default.image', 2) 将不再使用最新版本的 Firefox 来禁用图像 - [原因见 Alecxe 对我的问题的回答Can't turn off images in Selenium / Firefox ]

我拥有的最佳解决方案是使用 firefox 扩展 quickjava,除其他外,它可以禁用图像- https://addons.mozilla.org/en-us/firefox/addon/quickjava/

我的 Python 代码:

 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)

其他的东西也可以通过添加以下行来关闭:

  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)  ## Silverlight

关于javascript - 在 Selenium Python 中禁用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214473/

相关文章:

javascript - 在不改变其他属性的情况下动态改变特定文本的背景颜色

html - 根据父类有选择地隐藏子元素

javascript - 处理 reactjs .reduce 中的空值

javascript - 使用 Serialport nodejs 模块向客户极显示 COM2 发送命令

python - ast python如何在不同文件的方法之间找到连接

python - one-hot 编码 : list of column_values has to encode

python - 使用 ctypes 将字符串作为 char 指针数组传递

CSS 获取一层覆盖另一层

javascript - 使用 websocket 的聊天应用程序

javascript - AngularJS-文本不与 Jquery 时间选择器绑定(bind)