【问题标题】:Check if element is clickable in Selenium Java检查 Selenium Java 中的元素是否可点击
【发布时间】:2016-11-14 14:05:58
【问题描述】:

我是Selenium 的新手,需要检查Selenium Java 中的元素是否可点击,因为element.click() 会同时传递linklabel

我尝试使用以下代码但不工作:

WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10);

if(wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")))==null)

【问题讨论】:

    标签: java selenium selenium-webdriver automation


    【解决方案1】:

    elementToBeClickable 用于检查元素是否可见并已启用,以便您可以单击它。

    ExpectedConditions.elementToBeClickable 返回 WebElement 如果预期条件为真,否则将抛出 TimeoutException,它永远不会返回 null

    因此,如果您使用ExpectedConditions.elementToBeClickable 找到一个总是为您提供可点击元素的元素,那么无需检查null 条件,您应该尝试如下:-

    WebDriverWait wait = new WebDriverWait(Scenario1Test.driver, 10); 
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
    element.click();
    

    正如您所说,element.click() 同时传递linklabel 这并不意味着元素不可点击,这意味着返回元素clicked 但可能没有通过点击操作对元素执行事件.

    注意:- 我建议您始终先尝试通过idnameclassName 和其他定位器查找元素。如果您在查找时遇到困难,请使用 cssSelector 并始终将 xpath 定位器放在最后,因为它比其他定位器定位元素要慢。

    希望对你有帮助..:)

    【讨论】:

    • 那么我应该使用监听器吗?检查标签上的任何事件
    • @SandeepKrishnappa 不需要,您应该尝试先在浏览器控制台上使用 javascript 在标签处执行点击。并验证它是否接收到任何点击事件...如果接收到,那么它肯定会被 selenium 点击..
    • @SaurabhGaur - 我有一个类似的问题。我想知道你能不能替我回答?谢谢。 stackoverflow.com/questions/60762906/…
    【解决方案2】:

    在某些情况下,element.isDisplayed() && element.isEnabled() 将返回 true,但元素仍将可点击,因为它被其他元素隐藏/重叠。

    在这种情况下,Exception 被捕获为:

    org.openqa.selenium.WebDriverException:未知错误:元素不是 在点 (781, 704) 处可点击。其他元素会收到点击: <div class="footer">...</div>

    改用此代码:

    WebElement  element=driver.findElement(By.xpath"");  
    JavascriptExecutor ex=(JavascriptExecutor)driver;
    ex.executeScript("arguments[0].click()", element);
    

    它会起作用的。

    【讨论】:

    • 确实如此,大部分人都不知道这个事实!
    • @Rajagopalan : 没错,你的元素应该在 UI 中可见并且不应该有任何覆盖。
    • @cruisepandey 更清楚地阅读他的答案,他解释说element.isDisplayed() && element.isEnabled() will return true but still element will not be clickable
    • 我正在尝试用另一个库重新创建它,但我不确定我是否理解它是如何工作的。你能解释一下你的解决方案吗?
    【解决方案3】:

    wait.until(ExpectedConditions)不会返回null,要么满足条件要么抛出TimeoutException

    您可以检查元素是否显示和启用

    WebElement element = driver.findElement(By.xpath);
    if (element.isDisplayed() && element.isEnabled()) {
        element.click();
    }
    

    【讨论】:

    • 在某些情况下element.isDisplayed() && element.isEnabled() 将返回true 但仍然无法点击元素,因为元素被其他元素隐藏/重叠。在这种情况下,捕获的异常是:org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (781, 704). Other element would receive the click: <div class="footer">...</div>
    【解决方案4】:

    从源代码中可以看到ExpectedConditions.elementToBeClickable()会判断元素是否可见和启用,所以可以将isEnabled()isDisplayed()一起使用。以下是源代码。

    public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element) {
    		return new ExpectedCondition() {
    			public WebElement apply(WebDriver driver) {
    				WebElement visibleElement = (WebElement) ExpectedConditions.visibilityOf(element).apply(driver);
    
    				try {
    					return visibleElement != null && visibleElement.isEnabled() ? visibleElement : null;
    				} catch (StaleElementReferenceException arg3) {
    					return null;
    				}
    			}
    
    			public String toString() {
    				return "element to be clickable: " + element;
    			}
    		};
    	}

    【讨论】:

      【解决方案5】:

      有些事情你必须注意:

      • WebDriverWaitExpectedConditions 的结合为 elementToBeClickable() 一旦它被定位可点击即返回WebElement,即可见启用
      • 在此过程中,WebDriverWait 将忽略在until 条件下默认遇到的NotFoundException 实例。
      • 一旦 wait 的持续时间在所需元素未定位可点击上到期,将引发超时异常。
      • 解决此问题的不同方法是:
        • 要在元素返回后立即调用click(),您可以使用:

          new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))).click();
          
        • 要简单地验证元素是否定位可点击,请将WebDriverWait包装在try-catch{}块中,如下所示:

          try {
                 new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]")));
                 System.out.println("Element is clickable");
               }
          catch(TimeoutException e) {
                 System.out.println("Element isn't clickable");
              }
          
        • 如果WebDriverWait返回locatedclickable元素,但该元素仍然不可点击,则需要调用executeScript()方法如下:

          WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("(//div[@id='brandSlider']/div[1]/div/div/div/img)[50]"))); 
          ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
          

      【讨论】:

      【解决方案6】:
      List<WebElement> wb=driver.findElements(By.xpath(newXpath));
              for(WebElement we: wb){
                  if(we.isDisplayed() && we.isEnabled())
                  {
                      we.click();
                      break;
                  }
              }
          }
      

      【讨论】:

        【解决方案7】:

        当元素不可点击时,class 属性包含disabled

        WebElement webElement = driver.findElement(By.id("elementId"));
        if(!webElement.getAttribute("class").contains("disabled")){
            webElement.click();
        }
        

        【讨论】:

          【解决方案8】:

          有一个名为“cursor”的 CSS 属性,它决定了当鼠标指针悬停在元素上时要显示的鼠标光标类型。

          它是用来使元素可点击的,所以它可以用来检查一个元素是否可点击。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-01-12
            • 1970-01-01
            • 1970-01-01
            • 2018-05-14
            • 1970-01-01
            • 2011-08-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多