一尘不染

如何使用Selenium和Java单击reCaptcha

selenium

为什么在尝试让驱动程序单击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();

阅读 343

收藏
2020-06-26

共1个答案

一尘不染

要调用click()的验证码 复选框为元素中的<iframe>您需要:

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

* 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();
        }
    }
  • Browser Snapshot:

reCaptcha

2020-06-26