【问题标题】:Unable to Click on Continue button - Selenium Webdriver无法单击继续按钮 - Selenium Webdriver
【发布时间】:2014-01-04 20:15:35
【问题描述】:

无法使用 Selenium 单击链接 - Webdriver:Chrome:Win7

以下是我检查按钮时的代码:

a id="continue_button" disabled="false" class="button button-large ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" href="#" role="button" aria-disabled="false">

span class="ui-button-text"> span class="button-content">继续

我尝试了以下方法,但没有任何效果:

driver.findElement(By.xpath("//*[@id='continue_button']")).click();
driver.findElement(By.xpath("//a[contains(..,'Continue')]")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(\"a[id$='continue_button']\").click()");

【问题讨论】:

  • 等待按钮先在debug中尝试,然后尝试点击driver.verify().verifyElementPresent(by)

标签: selenium-webdriver


【解决方案1】:

也许链接部分隐藏在 DOM 中。您可以尝试使用下面的代码 sn-p 单击隐藏(或部分隐藏)的元素。

WebElement element = driver.findElement(By.xpath("//a[@id='continue_button']));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

【讨论】:

    【解决方案2】:
    options=driver.find_elements_by_class_name("button-content")
    for i in options:
        if(i.text=="Continue"):
            i.click()
    

    上面的代码是在python中的。通过翻译成java来尝试上面的代码,我希望它会完美运行......任何查询让我知道。

    【讨论】:

    • 如果你想要多个元素,你需要使用.find_elements_by_class_name() 来迭代一个列表,就像这里展示的那样。
    • 对不起,这是我的错误。。谢谢
    【解决方案3】:

    你可以试试这个,

    new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#continue_button span.button-content"))).click();
    

    上面的代码会等待 60 秒的元素可见性,如果找到,元素会被点击,否则会抛出异常。

    【讨论】:

      【解决方案4】:
      if(driver.getTitle().contains("Error"))
          driver.navigate().to("javascript:document.getElementById('overridelink').click()");
      

      【讨论】:

      • 最好解释一下为什么你的代码可以解决问题。见How to Answer
      【解决方案5】:

      请检查按钮是否位于框架中..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-01
        • 2016-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        • 2020-12-15
        • 2017-01-17
        相关资源
        最近更新 更多