Why am I getting errors when trying to get the driver to click on the reCaptcha
button?
this is the site where i am trying to get it to work: https://rsps100.com/vote/760/
This is my current code so far:
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();
To invoke click()
on the reCaptcha checkbox as the element is within an <iframe>
you need to:
Use WebDriverWait
to identify the element.See if this help.
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();
Here is the code that should work.
driver.switchTo().frame("a-9wt0e8vkopnm");
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
This worked for me. Please note that I am using Selenide. For regular selenium code look the same.
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();
}