【问题标题】:Selenium webelement clickable or not JAVASelenium webelement可点击或不可点击JAVA
【发布时间】:2021-07-09 11:32:23
【问题描述】:

所以 selenium 中没有isClickable() 函数。然而,isEnabled() 大部分时间都有效(与 isDisplayed() 结合使用),但并非总是如此。

我发现自己处于这种情况。我有一个灰显且不可点击的 btn(由 css 选择器找到),但我找不到任何方法来验证这一点。 (isEnabled 始终为真,并且始终显示)/ 元素本身没有与可点击的相同 btn 不同的属性或值。

我没有想法,我尝试了 try/catchexpectedConditions.elementToBeClickable(element),但也总是返回 true。

有没有人可以为我提供解决方案或其他方式? 也许我可以验证 CSS 位(以前从未做过)?这是一个有效的断言吗?

谢谢

【问题讨论】:

  • 当它是greyed out 并且它实际可用时,你能分享 HTML 代码吗?
  • codepen.io/thomarkey/pen/BaRzbwX启用的除了“值”之外是一样的
  • 我相信Currently not available 变灰时可见?我指的是data-nap字段
  • 启用的是一样的,除了“值”和像这样codepen.io/thomarkey/pen/LYyZaem的添加量字段@
  • 是的,谢谢@cruisepandey

标签: java selenium clickable


【解决方案1】:

当按钮可用时,我看到了不同之处,

<input class="form-control qty" name="qty" type="number" value="1" min="1">

您可以使用以下代码块来检查何时单击:

代码:

try {
    if (driver.findElements(By.xpath("//input[@name='qty' and @class='form-control qty' and @type='number']")).size()  > 0 ) {
        System.out.println("This means button is available to click");
        // code to click on button should be written here
    }
    else {
        System.out.println("Button must be greyed out, if you are seeing this");
        // do some stuff to make button available
    }
}
catch(Exception e) {
    System.out.println("Check the code again, looks like some issue. ");
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    灰色元素通常具有disabled 属性,而启用的元素是enabled 或只是缺少disabled 或类似的东西。在大多数情况下,这可能是一个明确的指示。

    【讨论】:

      【解决方案3】:
      Have you check this way  
      

      代码:

      WebElement element =(WebElement)((JavascriptExecutor)driver)
      .executeScript("return document.querySelector('JS Path')");
          
      if(element.isEnabled() && element.isDisplayed()){
      element.click();
      }else {
          //statement for not displayed   
      }
      

      【讨论】:

        猜你喜欢
        • 2018-05-14
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        • 2021-10-18
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 2015-06-13
        相关资源
        最近更新 更多