使用Selenium FirefoxDriver在Google搜索上禁用样式 [英] Disable styling on Google Search with Selenium FirefoxDriver

查看:110
本文介绍了使用Selenium FirefoxDriver在Google搜索上禁用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码禁用加载有Selenium Firefox Webdriver的页面上的样式表和图像:

The following code disables stylesheets and images on a page loaded with Selenium Firefox webdriver:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)

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

driver.close()


它在 stackoverflow.com facebook.com yahoo.com 上可以正常使用...但是有趣的是,在Google搜索中无法使用;只有Google徽标消失了,样式表仍然保留.


It works fine with stackoverflow.com, facebook.com, yahoo.com... but interestingly doesn't with Google Search; only the Google logo disappears and its stylesheet remains in place.

如果尝试使用以下链接http://google.com/search?q=nelson+mandela,您将获得:

If you try with the following link http://google.com/search?q=nelson+mandela, you will get:

预期结果应如下所示(无样式表+无图片):

Whereas the expected result should look like this (no stylesheet + no picture):

  • 发生了什么事?
  • 我该如何解决?

推荐答案

Google徽标来自CSS,其中图片作为数据嵌入到HTML中(img src ="data:image/jpeg; base64,.. ..)代码禁止加载不是此类来源的远程图像

The google logo come form the css, where the pictures are embedded in the HTML as data (img src="data:image/jpeg;base64, ....) the code disable the loading of remote images not this type of sources

  • permissions.default.stylesheet:禁用任何格式

  • permissions.default.stylesheet: disable any formatting

permissions.default.image:禁用任何图像和CSS背景图像

permissions.default.image: disable any image and css background-image

如果图像是作为base64编码嵌入到页面中的,则不会被这些权限阻止,因为这是HTML代码的一部分 (请参见 http://en.wikipedia.org/wiki/Data_URI_scheme )

if the image is embedded into the page as base64 encoded is not blocked by these permissions because is part of the HTML code (see http://en.wikipedia.org/wiki/Data_URI_scheme)

要禁用更多格式,您应该添加:

to disable more formatting you should add:

  • firefox_profile.set_preference("permissions.default.script",2);
  • firefox_profile.set_preference("javascript.enabled",False);

这篇关于使用Selenium FirefoxDriver在Google搜索上禁用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆