如何使用Selenium和Java点击reCAPTCHA

8

当我尝试点击reCAPTCHA按钮时,为什么会出现错误?

这是我正在尝试让它工作的站点: 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();
5个回答

13

在一个包含 <iframe> 的情况下,要在 reCaptcha 复选框 上调用 click() ,你需要执行以下操作:

  • 使用 WebDriverWait 等待所需的 frameToBeAvailableAndSwitchToIt
  • 使用 WebDriverWait 等待所需的 elementToBeClickable
  • 您可以使用以下解决方案:

    • 代码块:

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();
    }
}
  • 浏览器快照:

    reCaptcha


  • 1
    你总是偷我们的努力 :)。我已经运行了代码10次,它运行得很好,但 OP 抱怨。 - KunduK
    @KajalKundu 我很希望看到你成功,但是遵循最佳实践,以便新的贡献者能够得到最好的指导 :) 尽管如此,你的贡献非常有帮助。 - undetected Selenium
    SO(Stack Overflow)在你提供完整代码的时候真的很混乱,其他贡献者总是抱怨。而当你给出具体答案时,问题提问者(OP)也会感到困惑。 - KunduK
    3
    针对我的情况,我需要点击 'div.rc-anchor-content' 元素。 - gtiwari333
    有关通过下一部分的任何提示?选择正确的图像等吗?@KunduK - Phil Brockwell
    如评论中所提到的,我还需要 'div.rc-anchor-content' 以及以下内容: "//iframe[starts-with(@title, 'reCAPTCHA') and starts-with(@src, 'https://recaptcha.net')]" - Toofy

    3

    使用WebDriverWait来识别元素。看看这是否有帮助。

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-')]")));
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark")));
    element.click();
    

    我的 XPath 正确吗?我的 cssSelector 正确吗?我尝试了你的代码和我的代码组合 --> https://pastebin.com/g1pfzduq 但它显示“找不到元素”并在控制台中出现了很多红色错误。 - alqqu
    你自己试过了吗?我得到了这个错误 --- 预期条件失败:等待帧可用:By.xpath:// iframe [starts-with(@ name,'a-')](尝试30秒,间隔500毫秒)---原因是:没有这样的元素。 - alqqu
    你的框架是动态的,所以我写了这样的XPath。复制整个代码并切换框架。 - KunduK

    3

    这对我很有用。请注意,我正在使用Selenide。对于常规的selenium代码,看起来也是一样的。

        import static com.codeborne.selenide.Selenide.*;
    
        void recaptchaTest() {
    
            open("https://rsps100.com/vote/760");
    
            switchTo().frame($x("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"));
    
            $("div.rc-anchor-content").click();
    
            switchTo().defaultContent();
    
        }
    

    0

    也许我的解决方法很傻。但请记住,我不在测试环境中,并且正在练习测试自动化。所以这是我的解决方案:

    除此之外,使用

    public void notABot( ) {
    
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds( 15 ));
        wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name,'a-') and starts-with (@src, 'https://www.google.com/recaptcha')]")));
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div [ @class =  'recaptcha-checkbox-border']"))).click( );
        driver.switchTo().defaultContent();
    }
    

    除此之外,我还添加了自定义的发送按键方法

    public void inputEmail( ) {
    
        inputEmail.click( );
    
        String email = Strings.EMAIL_FOR_SIGNUP;
        for (int i = 0; i < email.length(); i++) {
    
            char c = email.charAt(i);
            String s = new StringBuilder( ).append( c ).toString( );
    
            inputEmail.sendKeys( s );
            sleepSendKeys( );
        }
    }
    

    睡眠时间为300毫秒。在96%的时间里,我成功地欺骗了google reCaptcha,实际上是一个人登录到页面。它对我很有效。


    0

    这里是应该能够正常工作的代码。

    driver.switchTo().frame("a-9wt0e8vkopnm");
    driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
    

    1
    “Exception in thread "main" org.openqa.selenium.NoSuchFrameException: No frame element found by name or id a-9wt0e8vkopnm”是我在尝试您的代码时遇到的错误。是的,我确保网站已正确加载直到执行这些命令,但仍然出现了错误。 - alqqu
    我认为那是正确的名称,但你能用ID试试吗? - supputuri

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接