【问题标题】:Selenium Webdriver - how to check if attribute is missing and then proceedSelenium Webdriver - 如何检查属性是否丢失然后继续
【发布时间】:2016-10-14 20:39:51
【问题描述】:

元素的“禁用”属性仅在启用按钮时出现。对于禁用状态,属性 disabled="disabled" 不会显示在 HTMl 中(属性本身而不是值)

如何检查属性,如果不存在则继续

我试过了

driver.findElement(By.id("button")).getAttr‌​ibute("disabled") != "disabled"

但是当按钮处于禁用状态时,此行会失败,因为没有“禁用”属性

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">

【问题讨论】:

  • 正如您所说的,当启用了“id= button”的按钮时,属性 disabled 存在。当按钮被禁用时,属性 disabled 不存在。那么你到底想要达到什么目的?请简化您的问题并为其添加更多 HTML 代码 sn-p。
  • 如果你尝试了 driver.findElement(By.id("button")).getAttr‌​ibute("disabled") != null .
  • 当我使用上面的内容时,我没有收到错误,但是 While 退出而不读取页面的详细信息。所以 Do While 循环并不能解决问题。无论如何,如果你以上回答我可以将您的答案设置为已接受

标签: java selenium webdriver


【解决方案1】:

请尝试

driver.findElement(By.id("button")).getAttr‌​ibute("disabled") != null;

【讨论】:

    【解决方案2】:

    试试这个方法,

    public boolean existsElement(By locator) {
    
        try {
    
            driver.findElement(locator);
    
        } catch (NoSuchElementException e) {
    
            return false;
        }
    
        return true;
    
    }
    

    如果 web 元素存在,它将返回 true,如果不存在,它将返回 false,具体取决于您可以继续的标志值。

    By lctr = By.xpath("//*[@id='button'][@disabled='disabled']");
    
    boolean flag = existsElement(lctr);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多