How to Get Attribute of Element from Selenium

How to get attribute of element from Selenium?

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")

How to get attribute value using Selenium - Python

To print the value of the title attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and class attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data='#results'] > a.btn.btn-white.btn-sm.btn-rounded.dropdown-toggle"))).get_attribute("title"))
  • Using XPATH and innerText:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data='#results']/a[text()='results']"))).get_attribute("title"))
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

Selenium I want to get a specific property of this Element

In case there are multiple elements (checkboxes) matching this locator you may get all them into a list and get each element attribute iterating over that list

List<WebElement> searchTextBoxes= driver.findElements(By.className(“ui-igcheckbox-container”));
for(WebElement searchTextBox : searchTextBoxes){
String typeValue=searchTextBox.getAttribute(“aria-checked”);
System.out.println("Value of type attribute: "+typeValue);
}

Selenium webdriver: How do I find ALL of an element's attributes?

It is not possible using a selenium webdriver API, but you can execute a javascript code to get all attributes:

driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)

Demo:

>>> from selenium import webdriver
>>> from pprint import pprint
>>> driver = webdriver.Firefox()
>>> driver.get('https://stackoverflow.com')
>>>
>>> element = driver.find_element_by_xpath('//div[@class="network-items"]/a')
>>> attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)
>>> pprint(attrs)
{u'class': u'topbar-icon icon-site-switcher yes-hover js-site-switcher-button js-gps-track',
u'data-gps-track': u'site_switcher.show',
u'href': u'//stackexchange.com',
u'title': u'A list of all 132 Stack Exchange sites'}

For completeness sake, an alternative solution would be to get the tag's outerHTML and parse the attributes using an HTML parser. Example (using BeautifulSoup):

>>> from bs4 import BeautifulSoup
>>> html = element.get_attribute('outerHTML')
>>> attrs = BeautifulSoup(html, 'html.parser').a.attrs
>>> pprint(attrs)
{u'class': [u'topbar-icon',
u'icon-site-switcher',
u'yes-hover',
u'js-site-switcher-button',
u'js-gps-track'],
u'data-gps-track': u'site_switcher.show',
u'href': u'//stackexchange.com',
u'title': u'A list of all 132 Stack Exchange sites'}

Selenium + Python: Print the text attribute of an element

To get the text Dual Complete Plan 1 you need to use

element.text

or

element.get_attribute("innerHTML")

or

element.get_attribute("textContent")

Instead of presence_of_element_located() use visibility_of_element_located()

and following css selector to identify

div[id*='Title'] > span.OSFillParent

Or

div.dividers.full-width > span.OSFillParent

Code:

        try:
Advantage = WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div[id*='Title'] > span.OSFillParent"))).text
except:
pass
print(Advantage )

Trouble getting attributes of elements using Selenium | Python

As per your question and the HTML you have shared it seems that the element is a React element, so to retrieve the attribute aria-label you have to induve WebDriverWait for the desired element to be visible and you can use the following solution:

 print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "element_xpath_you_found"))).get_attribute("aria-label"))

reference

Python Selenium can't get attribute from element found by class name

There are multiple class names there. To locate element by multiple class names you can use css_selector or XPath

find_element_by_css_selector('.css-1rovmyu.eanm77i0').get_attribute('src')

or

find_element_by_xpath('//img[class="css-1rovmyu eanm77i0"]').get_attribute('src')

Also you put a dot . between the class names. This is CSS syntax, no to be used when selecting by class name.

Also if you put one dot - put the second. Now it's a correct css_selector ;)

extracting attribute value of parent element using Selenium

You reversed the order of going to the parent element, and you need () in text. The xpath should be

"//span[text()='Geolocation']/../.."

Another option is to look for an element that has a chilled with "Geolocation" text

"//div[.//span[text()='Geolocation']]"

this might give you more results, depends on the html structure that is not in the question. In that case you can add unique attribute, for example tabgroup

"//div[.//span[text()='Geolocation']][@tabgroup]"

this will return only <div> tag that has tabgroup attribute.

To extract the data use getAttribute("class") on chill WebElement



Related Topics



Leave a reply



Submit