如何使用Selenium和Java单击reCaptcha [英] How to click on the reCaptcha using Selenium and Java

查看:215
本文介绍了如何使用Selenium和Java单击reCaptcha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么尝试让驱动程序单击reCaptcha按钮时出现错误?

Why am I getting errors when trying to get the driver to click on the reCaptcha button?

这是我试图使其正常运行的网站: https://rsps100.com/投票/760/

this is the site where i am trying to get it to work: https://rsps100.com/vote/760/

这是我目前的代码:

WebElement iframeSwitch = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div/div/div[2]/div/form/div/div/div/div/iframe"));
driver.switchTo().frame(iframeSwitch);   
driver.findElement(By.cssSelector("div[class=recaptcha-checkbox-checkmark]")).click();  

推荐答案

要在 reCaptcha 复选框上调用click(),因为该元素位于<iframe>中需要:

To invoke click() on the reCaptcha checkbox as the element is within an <iframe> you need to:

  • 为所需的 frameToBeAvailableAndSwitchToIt 诱导 WebDriverWait .
  • 为所需的 elementToBeClickable 诱导 WebDriverWait .
  • 您可以使用以下解决方案:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use the following solution:

  • 代码块:

  • Code Block:

public class ReCaptcha_click {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://rsps100.com/vote/760");
        new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")));
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
    }
}

浏览器快照:

这篇关于如何使用Selenium和Java单击reCaptcha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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