-2

I have an issue with located correct Xpath/ID input field, I am taking the following website as an example:

https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine

After you open the link then scroll down a bit, you will see "write a comment" blue button, click it then fill the text and name, it will appear a reCaptcha example, I tried the following way to click the checkbox, but without success. I would really appreciate if someone can let me a hand on it

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, 10)
    .until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
JeffC
  • 22,180
  • 5
  • 32
  • 55
ken
  • 1
  • 3

2 Answers2

0

Seems you were close enough. The name of the <iframe> i.e. a-x2cp4vfbaqu8 looks dynamic, so it would be better to avoid the name attribute and you can use either of the following Locator Strategies:

  • Using css_selector:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[@id='recaptcha-anchor']"))).click();
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you so much for the information, it is really useful. But I tried it, it still return to me Expected condition failed: waiting for an element to be clickable: By.cssSelector: //span[@id='recaptcha-anchor']. May I ask if you can give me some hints to solve this? Kind regards – ken Mar 01 '20 at 08:26
0

I struggled with this for several days, it's actually very simple. The captcha is located in a frame, and in order to click on it, you need to switch to this frame. I did it like this with Selenium:

driver.switchTo().frame(driver.findElement(By.xpath("//*[@title='reCAPTCHA']")));
driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
ahuemmer
  • 1,653
  • 9
  • 22
  • 29
Zenk0
  • 13
  • 1
  • 2