Welcome to Manicprogrammer Sign in | Join | Help

Finding out if a given selector (XPATH, ID or Whatever) is Enabled in Selenium

I just spent a whole day trying to figure out how to define if a given element (defined using an xpath) is an enabled (i.e. not disabled) element in "Selenese", using Python bindings.

Finally cracked it:

def is_element_enabled(self, element):
  script = """this.page().findElement("%s").disabled;"""
   
  script_return = self.selenium.get_eval(script % element)
  if script_return == "null":
  is_disabled = self.__get_attribute_value(element, "disabled")
  else:
  is_disabled = script_return[0].upper()=="T"
   
  return not is_disabled

I know it's not pretty but after that many hours I DON'T CARE! I'm hoping someone will come and say: "There's a much easier way of doing that. Just use xxx" and then I'll fix it. For the time being it stays.

BTW http://www.pyccuracy.org is up and running and Pyccuracy got it's 0.3.1 release already.

Cheers,

Published Wednesday, March 18, 2009 1:43 AM by heynemann
Filed under

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Finding out if a given selector (XPATH, ID or Whatever) is Enabled in Selenium

Wednesday, March 18, 2009 2:26 AM by Simon Stewart

In webdriver (and selenium 2.0):

WebElement e = driver.findElement(By.id("foo"));

e.isEnabled();

Getting the value of the "disabled" attribute is the right way to go.

# re: Finding out if a given selector (XPATH, ID or Whatever) is Enabled in Selenium

Wednesday, March 18, 2009 8:55 AM by Bernardo Heynemann

Hey Simon! Thanks for the comment!

Yeah I tried getting the value of @disabled. It does not work at all times though :(

The code I'm using now works.

WebElement e = driver.findElement(By.id("foo"));

e.isEnabled();

that's java right?

How would I do that in the Python bindings?

Cheers,

Bernardo Heynemann

# Finding out if a given selector (XPATH, ID or Whatever) is Enabled in Selenium | Planeta Globo.com


Enter the text you see in the image:

Leave a Comment

(required) 
required 
(required)