Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isElementPresent is very slow in case if element does not exist.

Tags:

I am using below code to check for element on my web page

private boolean isElementPresent(By by) {
try {       
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
        return false;
    }
    catch (Exception e)
    {       
        return false;
    }

  }

I need to check in my program if a particular region appears in result as below

isElementPresent(By.xpath(".//*[@id='header']")));

If this is present this function completes quickly but if above is not present then it run for very long.

Could some one please help me in resolving this issue so that this check can be performed quickly?

like image 754
user419534 Avatar asked Apr 18 '13 06:04

user419534


People also ask

How do you handle if an element is not present in Selenium?

We can verify if an element does not exist in Selenium webdriver. To achieve this, we shall use the method getPageSource which gets the entire page source. So we can obtain a complete page source and check if the text of the element exists.

How do you handle if an element is not present in Selenium Python?

We can introduce a try / except block. In the except block, we shall throw the NoSuchElementException in case the element does not exist on the page. We can also verify if an element is present in the page, with the help of find_elements() method. This method returns a list of matching elements.

How will you validate if an element is not displayed in Selenium?

isDisplayed() If the element is displayed, then the value returned is true. If not, then the value returned is false. The code below verifies if an element with the id attribute value next is displayed.

Does element exist in Selenium?

We can verify whether an element is present or visible in a page with Selenium webdriver. To check the presence of an element, we can use the method – findElements. The method findElements returns a list of matching elements. Then, we have to use the method size to get the number of items in the list.


1 Answers

Here you are missing somethings that is why it is waiting If there is not element. findElement will wait for an element implicitly specified time. so need to set that time to zero in that method.

isElementPresent(WebDriver driver, By by) {  
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);  
    try {  
        driver.findElement(by);  
        return true;  
    } catch (NoSuchElementException e) {  
        return false;  
    } finally {  
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
    }  
}

There are 4 important things going on here. In order:

  1. Setting implicity_wait to 0 so that WebDriver does not implicitly wait.

  2. Returning True when the element is found.

  3. Catching the NoSuchElementException and returning False when we discover that the element is not present instead of stopping the test with an exception.

  4. Setting implicitly_wait back to 30 after the action is complete so that WebDriver will implicitly wait in future.

like image 156
Santoshsarma Avatar answered Sep 18 '22 08:09

Santoshsarma