Selenium WebDriver - determine if element is clickable (i.e. not obscured by dojo modal lightbox)

好久不见. 提交于 2019-11-27 15:28:26
Pavel Zorin

Use the WebDriverWait conditions.

    WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));

Webdriver will wait for 5 seconds for your element to be able to be clicked.

You can use the ExpectedConditions.invisibilityOfElementLocated(By by) method which waits until the element is either invisible or not present on the DOM.

WebDriverWait wait = new WebDriverWait(yourWebDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("yourSavingModalDialogDiv")));

So, depending on how much time your modal dialog takes to go invisible or go off the DOM, webdriver will wait. The wait is for a maximum of 10 seconds.

You could create a clickUntil function/method that does a WebDriver wait until the element is clickable with a timeout. It would attempt to click on the element, and throw away "Element is not clickable" error messages each time until it becomes clicked or times out.

Not sure how to write that in dojo, but that's an idea.

I also have same problems, but I tested many input in site. One are clickable which I tested and other - not clickable one just skipped. I made it by try() catch() Simply Code :

for(){ // for all my input
try {
    driver.findElement(By.xpath("...."
                                + "//input)["+(i+1)+"]")).click();

  ... tests...


} catch(Exception e) {
     if(e.getMessage().contains("is not clickable at point")) {

          System.out.println(driver.findElement(By.xpath(locator)).
          getAttribute("name")+" are not clicable");
     } else {
          System.err.println(e.getMessage());
     }
}

And more elegant:

 @SuppressWarnings("finally")
       public boolean tryClick(WebDriver driver,String locator, locatorMethods m) {

           boolean result = false;
           switch (m) {

           case xpath:
            try {
                driver.findElement(By.xpath(locator)).click();
                result= true;
            } catch (Exception e) {
                   if(e.getMessage().contains("is not clickable at point")) {
                       System.out.println(driver.findElement(By.xpath(locator)).getAttribute("name")+" are not clicable");
                   } else {
                       System.err.println(e.getMessage());
                   }
            } finally {
                break;
            }
        case id:
            try {   
                driver.findElement(By.id(locator)).click();
                return true;
            } catch (Exception e) {
               if(e.getMessage().contains("is not clickable at point")) {
                   System.out.println(driver.findElement(By.id(locator)).getAttribute("name")+" are not clicable");   
               } else {
                   System.err.println(e.getMessage());
               }
            } finally {
                break;
            }

          default:
              System.err.println("Unknown locator!");

        }
           return result;
  }
nexoma

In Scala:

  1. Standard code for waiting (visibility/invisibility)

    (new WebDriverWait(remote, 45)).until(
        ExpectedConditions.visibilityOf(remote.findElement(locator))
    )
    Thread.sleep(3000)
    
  2. More visibility in logs :

    while (remote.findElement(locator).isDisplayed) {
        println(s"isDisplayed: $ii $a : " + remote.findElement(locator).isDisplayed)
        Thread.sleep(100)
    }
    
  3. If you have asynchronous JavaScript processes use web-elements with timestamp:

    val oldtimestamp = remote.findElements(locator).get(0).getAttribute("data-time-stamp")
    
    while (oldtimestamp == remote.findElements(locator).get(0).getAttribute("data-time-stamp")) {
        println("Tstamp2:" + remote.findElements(locator).get(0).getAttribute("data-time-stamp"))
        Thread.sleep(200)
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!