【问题标题】:How to check for disabled state of element and then continue in selenium webdriver如何检查元素的禁用状态,然后在 selenium webdriver 中继续
【发布时间】:2015-03-28 04:23:23
【问题描述】:

我有这个下一个按钮,脚本一直单击该按钮,直到它到达最后一页。它应该读取并比较每个页面中的数据,然后在禁用按钮后退出循环。当下一个按钮处于启用状态时,它读取数据并进行比较没有问题,但是当下一个按钮被禁用时,脚本不会读取该页面

这是元素

这是我的代码

do {
....
....

      if(driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).isDisplayed()) {

      driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).click();

        } else {

        }           
   } while(driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).isDisplayed());

问题是当到达第二页时,next 按钮被禁用并且 While 退出而不读取第 2 页中的数据。

如何确保页面被阅读

  1. 当只有一页数据时(所以下一个按钮将在开始时被禁用)

  2. 到达最后一页时,(下一个按钮变为禁用状态)

我曾尝试过 FluentWait,但失败了,如此处所述https://stackoverflow.com/questions/29282178/selenium-fluentwait-method-failing-to-ignore-nosuchelement

EDIT..为按钮的禁用状态添加了 HTML

<td>
    <input id="ReportViewer1_ctl06_ctl00_Next_ctl01_ctl00" type="image" 
style="border-style: none; height: 16px; width: 16px; border-width: 0px; cursor: default;"
 alt="Next Page" src="/xxxx Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=
0.0.30319.1&Name=Microsoft.Reporting.WebForms.Icons.NextPageDisabled.gif" 
title="Next Page" disabled="disabled" name="ReportViewer1$ctl06$ctl00$Next$ctl01$ctl00">

再次编辑 --- 启用按钮时的 HTML(没有禁用属性)

<input id="ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00" type="image" style="border-style: 
none; height: 16px; width: 16px; border-width: 0px;" alt="Next Page" 
src="/xxxxx/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=10.0.30319.
1&Name=Microsoft.Reporting.WebForms.Icons.NextPage.gif"
 title="Next Page" name="ReportViewer1$ctl06$ctl00$Next$ctl00$ctl00">

【问题讨论】:

    标签: java selenium webdriver


    【解决方案1】:

    您需要使用isEnabled() 检查而不是isDisplayed()

    【讨论】:

    • 当我执行 isEnabled() .....
    • @sunjose 你确定元素ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00 被禁用了吗?当按钮被禁用时,你能分享页面的 HTML 代码吗?谢谢。
    • 对不起,我的意思是 isEnabled.. 刚刚更正了我上面的陈述......所以如果按钮看起来像已禁用,那么对我来说没有意义,那么 isEnabled 怎么会一直找到按钮
    • @sunjose hm,看起来不错。你可以试试driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).getAttribute("disabled") != "disabled" 条件吗?
    • 我已经为启用状态添加了 HTML。它没有“禁用”属性...所以当状态启用时这条线会失败。我试过了,我得到了 Page 1 两次然后无穷无尽的页面 2.. 永远不会退出 while 语句
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多