Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Webdriver to check if element does NOT exist takes time

Trying to verify after few GUI operations some button does not exist (expected not to be present). I am using find_element_by_xpath() but its very slow. Any solution of timeout?

like image 306
Abhishek Kulkarni Avatar asked Oct 09 '12 05:10

Abhishek Kulkarni


People also ask

How do you check if element does not exist Selenium Python?

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. We can get the size of the list with the len method. If the len is greater than 0, we can confirm that the element exists on the page.

What is implicit wait in Selenium Python?

Implicit Waits. An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0 (zero). Once set, the implicit wait is set for the life of the WebDriver object.

Why is Selenium execution slow?

The Selenium WebDriver scripts are very slow because they run through the browser. There are multiple things that can improve the Selenium WebDriver scripts' speed: use fast selectors. use fewer locators.


1 Answers

Actually WebDriver's find_element method will wait for implicit time for the element if the specified element is not found.

There is no predefined method in WebDriver like isElementPresent() to check. You should write your own logic for that.

Logic

public boolean isElementPresent()
{
   try
   {
      set_the_implicit time to zero
      find_element_by_xpath()
      set_the_implicit time to your default time (say 30 sec)
      return true;
   }
   catch(Exception e)
   {
       return false;
   }
}

See : http://goo.gl/6PLBw

like image 93
Santoshsarma Avatar answered Oct 19 '22 10:10

Santoshsarma