Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute of element from Selenium?

I'm working with Selenium in Python. I would like to get the .val() of a <select> element and check that it is what I expect.

This is my code:

def test_chart_renders_from_url(self):     url = 'http://localhost:8000/analyse/'     self.browser.get(url)     org = driver.find_element_by_id('org')     # Find the value of org? 

How can I do this? The Selenium docs seem to have plenty about selecting elements but nothing about attributes.

like image 258
Richard Avatar asked May 19 '15 11:05

Richard


People also ask

How can the user find the element by attribute in Selenium?

We can find an element using the attribute name with Selenium webdriver using the locators - name, css, or xpath. To identify the element with css, the expression should be tagname[name='value'] and the method to be used is By. cssSelector.

What is the return type of getAttribute () and getText ()?

The getText() method simply returns the visible text present between the start and end tags (which is not hidden by CSS). The getAttribute() method on the other hand identifies and fetches the key-value pairs of attributes within the HTML tags.


1 Answers

You are probably looking for get_attribute(). An example is shown here as well

def test_chart_renders_from_url(self):     url = 'http://localhost:8000/analyse/'     self.browser.get(url)     org = driver.find_element_by_id('org')     # Find the value of org?     val = org.get_attribute("attribute name") 
like image 166
Saifur Avatar answered Oct 03 '22 13:10

Saifur