【问题标题】:How to click on the reCaptcha checkbox如何点击 reCaptcha 复选框
【发布时间】:2020-02-29 19:19:42
【问题描述】:

我在定位正确的 Xpath/ID 输入字段时遇到问题,我以以下网站为例:

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

打开链接后向下滚动一点,你会看到“写评论”蓝色按钮,点击它然后填写文本和名称,它会出现一个reCaptcha example,我尝试了以下方式点击复选框,但没有成功。如果有人可以帮我解决一下,我将不胜感激

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();

【问题讨论】:

    标签: java selenium-webdriver recaptcha


    【解决方案1】:

    看来你已经够近了。 <iframe>namea-x2cp4vfbaqu8 看起来是动态的,因此最好避免使用 name 属性,您可以使用以下Locator Strategies:

    • 使用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();
      
    • 使用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();
      

    参考文献

    您可以在以下位置找到一些相关讨论:

    【讨论】:

    • 非常感谢您提供的信息,它真的很有用。但是我试过了,它仍然返回给我预期的条件失败:等待元素可点击:By.cssSelector://span [@id ='recaptcha-anchor']。请问您是否可以给我一些提示来解决这个问题?亲切的问候
    【解决方案2】:

    我为此苦苦挣扎了好几天,其实很简单。验证码位于一个框架中,要点击它,您需要切换到该框架。 我用 Selenium 这样做了:

    driver.switchTo().frame(driver.findElement(By.xpath("//*[@title='reCAPTCHA']")));
    driver.findElement(By.xpath("//span[@id='recaptcha-anchor']")).click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2021-11-01
      • 2018-09-27
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多