我尝试使用 selenium webdriver 在 google 中执行一个按图像搜索,这样我的用户就不需要手动打开浏览器并将图像 url 粘贴到那里。但谷歌说
Our systems have detected unusual traffic from your computer network. This page checks to see if it's really you sending the requests, and not a robot.
并给出验证码,有没有办法避免被 google 使用 selenium webdriver 检测为自动化?
这里是我的代码:
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://images.google.com/searchbyimage?image_url=";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test2() throws Exception {
driver.get(baseUrl + "http://somesite.com/somepicture.jpg");
driver.findElement(By.linkText("sometext"));
System.out.println("finish");
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
最佳答案
谷歌似乎检测浏览器配置文件以确定是否发生了奇怪的事情。例如,如果您使用浏览器进行隐私浏览(我使用 firefox 和 chrome 对其进行了测试),您的浏览器配置文件将更改为匿名,因此 google 会发现它可疑并要求您填写验证码。
当您从 selenium webdriver 运行浏览器时,也会发生这种情况。
所以你需要使用像这样的代码将 selenium 驱动程序配置文件设置为默认配置文件(目前只适用于 firefox)
ProfilesIni allProfiles = new ProfilesIni();
WebDriver driver = new FirefoxDriver(allProfiles.getProfile("default"));
关于java - 如何避免谷歌搜索将 selenium webdriver 检测为异常行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28879950/