我使用以下URL浏览firefox(不要运行它,它被列入黑名单,我只是用于安全测试)。这是我使用的简单代码:
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("http://addonrock.ru/Debugger.js")
但我没有得到我想要的东西。我的意思是,当我在Firefox上自己输入这个URL时,我得到了这个页面:
但是当我使用上面的URL启动Firefox时,我宁愿得到这个页面:
如何用我的代码获得与图1相同的结果?提前感谢您的帮助。
答案 0 :(得分:3)
这是browser.safebrowsing.malware.enabled
Firefox首选项控制的内容。在Firefox浏览器中的about:config
中更改它会再现您所描述的行为:如果它已打开 - Firefox会警告您关于网站being in the malware-detected block list,如果它已关闭 - 您会看到Firefox未加载一页。
而且,仅供参考,这是安全性偏好设置中的“阻止报告的攻击网站”复选框:
通过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和python client向Google安全浏览发出请求(API密钥可以生成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'}