【问题标题】:Using Click() on href Button HTML在 href 按钮 HTML 上使用 Click()
【发布时间】:2023-03-27 03:20:01
【问题描述】:

我正在做一个 selenium 项目,在这个项目中,一旦它填写了一个文本框,点击下一页,就会出现一个验证码。首先你需要验证,然后验证码就会出现。它只是页面中间的一个弹出框,它使其他所有内容都无法点击,并在其后面的页面上覆盖着一层白雾。单击验证后,我已经有了解决验证码的方法。 我遇到的问题是,使用我尝试过的方法,webdriver 似乎找不到这个按钮的任何痕迹。在尝试使用click() 时,我总是得到no such element exception。我试过 wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();, wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("home_children_button")))).click(); ,我也试过在谷歌上放一个 thread.sleep(Num) 而不是等待,以及许多其他方法。然后当我对href按钮进行一些研究时,我发现人们推荐使用By.linkText。所以我尝试了我之前尝试过的所有方法,但仍然无法找到元素。我在谷歌上找不到其他任何东西,所以我想我会寻求帮助。 我已经附加了我目前正在查看的按钮的 HTML 代码,以及网站的链接。不幸遇到此页面,您必须注册一个帐户。这个页面通常会在我放入 DOB 后出现。感谢您提供的任何澄清。

<a href="javascript:;" id="home_children_button" class="sc-bdfBwQ jXXvQg sc-kEjbxe hVNnSn">Verify</a>

https://account.battle.net/creation/flow/creation-full

我尝试过的代码:

        wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("fc-iframe-wrap")));
        wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
        WebDriverWait wait2 = new WebDriverWait(driver, 10);
        wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("CaptchaFrame")));
        wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("home_children_button"))).click();

wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();,

wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("home_children_button")))).click();

Thread.sleep(5000) //wait for it to pop up
driver.findElement(By.id("home_children_button"))).click();

同时尝试使用By.linkText

解决方案: 在被告知这是一个 iframe 后,我仔细查看了 HTML,发现有多个 iframe。所以我等着切换它们才能点击按钮。

        WebDriverWait wait2 = new WebDriverWait(driver, 20);
        wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div[4]/iframe")));
        wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div/div[1]/div/iframe")));
        wait2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/body/div[1]/div[2]/div[1]/iframe")));
        
        wait2.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();

【问题讨论】:

    标签: java selenium href captcha


    【解决方案1】:

    正如我所怀疑的,它在 iframe 中,您需要切换驱动程序的焦点以与 verify 按钮进行交互:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("CaptchaFrame")));
    wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();
    

    一旦你完成它,你可能想要使用defaultContent()

    driver.switchTo().defaultContent();
    

    【讨论】:

    • 嗯,还是不行。我浏览了 HTML 并找到了验证码框架,但 webdriver 仍然无法显示。
    • ID 可能是“fc-iframe-wrap”吗?
    • 你能分享你一直在尝试的代码吗?
    • 我已经用我尝试过的内容更新了帖子。我尝试的大部分操作都是因为By 命令是No such Element Exception 而弄乱了它。
    • 我想通了!我会用我的方式更新帖子。
    猜你喜欢
    • 2020-11-13
    • 2015-08-06
    • 2017-09-04
    • 1970-01-01
    • 2014-05-19
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多