【问题标题】:How to click on the reCAPTCHA using Selenium and Java如何使用 Selenium 和 Java 点击 reCAPTCHA
【发布时间】:2019-08-11 08:25:35
【问题描述】:

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

【问题讨论】:

    标签: selenium selenium-webdriver iframe recaptcha webdriverwait


    【解决方案1】:

    要在 reCaptcha checkbox 上调用 click(),因为元素位于 <iframe> 内,您需要:

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

      • 代码块:

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

    【讨论】:

    • :你总是偷走我们的努力 :)。我已经运行了 10 次代码,它运行良好,但 OP 抱怨。
    • @KajalKundu 我很想看到你成功,但是遵循最佳实践是的,所以新的贡献者会以最好的方式得到指导:) 你的贡献仍然非常有帮助。
    • 当您提供整个代码时,其他贡献者总是抱怨这真的很令人困惑。当您给出具体答案时,OP 也会感到困惑:)
    • 就我而言,我必须点击“div.rc-anchor-content”元素
    【解决方案2】:

    这对我有用。请注意,我使用的是 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();
    
        }
    

    【讨论】:

      【解决方案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 对吗?我尝试使用您的代码和我的代码组合 --> pastebin.com/g1pfzduq 但它说“找不到元素”并且控制台中有很多红色错误
      • 你自己试过了吗?我收到此错误 --- 预期条件失败:等待帧可用:By.xpath://iframe[starts-with(@name,'a-')](尝试 30 秒,500 毫秒间隔)---由:没有这样的元素
      • 你的框架是动态的,所以我写了这样的xpath。用框架切换复制整个代码
      【解决方案4】:

      这是应该可以工作的代码。

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

      【讨论】:

      • 线程“主”org.openqa.selenium.NoSuchFrameException 中的异常:没有按名称或 id a-9wt0e8vkopnm 找到框架元素,这是我在尝试您的代码时得到的。是的,我让网站正确加载,直到我执行了这些命令但仍然出现错误
      • 我觉得这个名字是对的,但是你可以用 ID 试试。
      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 2020-07-14
      • 1970-01-01
      • 2020-04-28
      • 2023-03-19
      • 2019-05-23
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多