谷歌阻止了Selenium Webdriver

时间:2014-11-02 20:24:01

标签: python selenium

我有一个脚本,我试图通过selenium搜索谷歌页面来测试一些东西。每当我打开Webdriver时,我都会获得验证码表格:

fp = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=fp)
driver.get('https://www.google.com/search?q=asdf')

但是,如果我在浏览器中打开完全相同的页面https://www.google.com/search?q=asdf,它可以正常工作。为什么Google会提升验证码,以及我可以使用webdriver发送哪些参数,使其看起来像普通浏览器并且不会引发验证码?

注意,我已尝试添加我的用户代理,但它仍会引发同样的错误:

fp = webdriver.FirefoxProfile()
fp.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:32.0) Gecko/20100101 Firefox/32.0")
driver = webdriver.Firefox(firefox_profile=fp)

以下是来自普通浏览器的我的请求标头的示例:

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要设置用户代理。 见SO ANSWER

使用set_preference

答案 1 :(得分:0)

使用请求传递所有标头:

headers = {
    "Host": "www.google.com",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:32.0) Gecko/20100101 Firefox/32.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "Cookie": "PREF=ID=0df7e6fbda0c09d3:U=bfc47b624b57a0e9:FF=0:TM=1414961297:LM=1414961298:S=2FtJad1BEeJ0M5XS; NID=67=t5zTrFVtG4cLZH2kVmsQEbqDRFJisM86z1s27zx0A6vTR0MWqg69DaY39muso6fIEgqnli7IaEv1Rge1ZxBG0Nr1_3KH1aLu_z1-Ar48oiVDFFSVX4KDRgWnHQWjUfHC",
    "Connection": "keep-alive",
    "Cache-Control": "max-age=0",
}