Getting All Attributes from an Iwebelement With Selenium Webdriver

Getting ALL attributes from an IWebElement with Selenium WebDriver

The .attributes property in JavaScript will return an array of all the attributes a given element has and it's value.

So what you'll need to do is first get a driver that has the capability to run JavaScript:

IJavascriptExecutor javascriptDriver = (IJavaScriptExecutor)driver;

Now, execute it by:

Dictionary<string, object> attributes = javascriptDriver.ExecuteScript("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) as Dictionary<string, object>;

The idea behind the JavaScript is to use the JavaScript attributes property within the element itself and then pull out the information we need - the name and value of the attribute. The attributes property, in reality, pulls a lot of information about each individual property but we want only two fields. So we get those two fields, put them into a dictionary and WebDriver will then parse it back to us. (It could probably be cleaned up a bit)

It's now a Dictionary and thus you can loop through however you like. The key of each pair will be the name of the attribute, and the value of each pair will be the value of the attribute.

Only tested this with a few elements dotted around the web (here, Google, and a few small web pages) and it seems to work well.

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'}

Get a list of attribute values of all elements with specific class using Selenium in C#

Assuming that you need to fetch the value of "title" attribute from all elements with class name "Class". You can use FindElements method of WebDriver to get all elements with class name of "Class". From the list of all elements you can retrieve the attribute (title) value. I have also added a where clause to get rid of all the elements which has a class name of "Class" and has a empty "title" attribute.

    var alltitles = webDriver
.FindElements(By.ClassName("Class"))
.Where(x => !string.IsNullOrEmpty(x.GetAttribute("title")))
.Select(x => x.GetAttribute("title"))
.ToList();

How to Get this ID

Unfortunately you won't be able to access this field because the method "FindElements" returns the elements in the form of IWebElement. IWebElement does not have a method implemented to get the ID value you are looking for.

If the FindElements method was to return the type RemoteWebElement, or even ChromeWebElement, we would be able to access this field because the RemoteWebElement has a method to access it. However, this method is not implemented in the interface. So there is no way for us to get it. I've played around with casting and haven't been able to cast in the correct spot. So as of now, I'm not seeing a way to get this ID.

See RemoteWebElement.cs source code for more information:
https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Remote/RemoteWebElement.cs

Hopefully this helps you out a bit

How to extract the dynamic values of the id attributes of the table elements using Selenium and Java

To print the List of id attribute of the element you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • cssSelector:

    List<String> myID = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("td.journalTable-journalPost>a.htext-small"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
    System.out.println(myIDs);
  • xpath:

    List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//td[@class='journalTable-journalPost']/a[@class='htext-small' and text()='Download']"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
    System.out.println(myIDs);


Related Topics



Leave a reply



Submit