python - Selenium:页面显示不同

标签 python firefox selenium python-3.x selenium-webdriver

我使用以下 URL 启动 Firefox(不要运行它,它已列入黑名单,我仅使用它进行安全测试)。这是我使用的简单代码:

from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://addonrock.ru/Debugger.js") 

但是我没有得到我想要的。我的意思是,当我通过自己在 Firefox 上输入来运行此 URL 时,我会看到此页面:


enter image description here

但是当我使用上面的 URL 启动 Firefox 时,我宁愿看到这个页面:

enter image description here

如何使用我的代码获得与图1相同的结果?预先感谢您的帮助。

最佳答案

这是由 browser.safebrowsing.malware.enabled Firefox 首选项控制的。在 Firefox 浏览器内的 about:config 中更改它会重现您所描述的行为:如果打开 - Firefox 会警告您有关该网站 being in the malware-detected block list 的信息。 ,如果它关闭 - 您会看到 Firefox 根本不加载页面。

并且,仅供引用,这是安全首选项中的“阻止报告的攻击站点”复选框: firefox security prefences

通过 FirefoxProfile 设置 Firefox 首选项应该会有所帮助:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.safebrowsing.enabled', True)
profile.set_preference('browser.safebrowsing.malware.enabled', True)

driver = webdriver.Firefox(profile)
driver.get("http://addonrock.ru/Debugger.js")

您也可以关注this answer ,获取用户配置文件的路径并将其传递给 FirefoxProfile 构造函数。

就我个人而言,我无法通过设置特定首选项或传递现有配置文件来完成这项工作。我怀疑这可能会被报告为 the selenium bugtracker 中的问题.

<小时/>

如果您想测试某个网站是否被“标记”为危险网站,您可以通过其 API 向 google safebrowsing 发出请求。和 python client (可以生成API key here):

>>> key = 'your own key'
>>> from safebrowsinglookup import SafebrowsinglookupClient
>>> client = SafebrowsinglookupClient(key)

>>> client.lookup('http://addonrock.ru/Debugger.js')
{'http://addonrock.ru/Debugger.js': 'malware'}

>>> client.lookup('http://google.com')
{'http://google.com': 'ok'}

关于python - Selenium:页面显示不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25040984/

相关文章:

python - PyODBC 输出不正确的 UTF-16

python - 从扩展名之前的文件名中删除日期

Javascript Easing 在 Firefox 中不起作用

java - 通过 selenium 选择一个 javascript 下拉列表?

java - 如何等待具有相同类名的所有元素?

python - Python原始套接字更改符号

python - Pandas :将系列的数据类型更改为字符串

firefox - Headless 中的 Selenium FirefoxDriver 出现错误

selenium - 为 Firefox 创建一个假的网络摄像头流

java - Selenium 和 Eclipse_java 以及 firefox 测试问题