【问题标题】:Element not visible with button click in selenium chrome driver在 selenium chrome 驱动程序中单击按钮不可见元素
【发布时间】:2019-08-14 05:49:06
【问题描述】:

我正在尝试在填写此网页上的字段后单击继续按钮。但是即使我最大化屏幕并且您可以清楚地看到按钮,也会抛出一个异常说元素不可见。

即使页面等待 10 秒,我也尝试过以下操作:

driver.findElement(By.xpath("//*[@id=\"submitButton\"]/span")).click();
driver.findElement(By.cssSelector("#submitButton > span")).click();
driver.findElement(By.partialLinkText("Continue")).click();
driver.findElement(By.className("caret_rebrand")).click();
driver.findElement(By.name("submitButton")).click();

driver.findElement(By.xpath("//span[contains(@class,'red') and contains(text(), 'Continue')]")).click();

这是我尝试访问的 html 部分:

<button style="padding-left: 0px;" type=button" "id=submitButton" class = "nbutton" title = "Continue" onclick=_hblicnk('continue,'continue'); goFeaturePage('true');">
 <span style = "padding-right: 12px;" class="red"
      "Continue"
      <img class="caret_rebrand">
 </span>

我希望找到并单击继续按钮。 attached is the picture of the webpage

更新:8-3-19:我已经测试了以下代码片段,它能够在所有情况下找到元素。但是当将 .click() 函数添加到其中任何一个时,会导致 no such element 异常。

driver.findElement(By.name("submitButton")).click();
         driver.findElement(By.id("submitButton")).click();
         driver.findElement(By.cssSelector("#submitButton")).click();
         driver.findElement(By.xpath("//*[@id=\"submitButton\"]")).click();

【问题讨论】:

  • 检查按钮是否存在于 iframe 中。
  • javascript 中有 2 个 iframe 元素,我收到此错误。 org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击:By.xpath://button[@title='Continue(以 500 毫秒间隔尝试 60 秒)
  • 当你说有两个iframe时,如果这个按钮在任何一个iframe下,那么你需要先切换到那个iframe,然后再尝试对该按钮进行操作。
  • 我已经在所有 iframe 中测试了该按钮,但单击不起作用导致没有此类元素异常。当我运行以下代码时,它可以找到没有 iframe 的元素,但是当添加 .click() 函数时,它会停止工作。

标签: selenium-webdriver exception button visible


【解决方案1】:
  1. 我的期望是您需要单击&lt;button&gt; 元素,因为此&lt;span&gt; 可能根本无法单击。您可以使用即title 属性来唯一标识页面上的元素
  2. 最好使用Explicit Wait 来确保按钮存在且可点击,因为它可能在DOM 中不可用。查看How to use Selenium to test web applications using AJAX technology文章了解更多详情
  3. 假设以上所有我相信你应该能够使用下面的代码来点击按钮:

    continue_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Continue']")))
    continue_button.click()        
    

【讨论】:

  • 我尝试使用你的方法,它给了我这个例外。 org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击:By.xpath://button[@title='Continue(以 500 毫秒间隔尝试 60 秒)
猜你喜欢
  • 2017-10-21
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2017-10-15
  • 2020-05-15
  • 2018-08-15
  • 2012-11-19
  • 1970-01-01
相关资源
最近更新 更多