ChrisGuest
ChrisGuest

Reputation: 3608

Get HTML attributes using splinter

Using splinter is there a simple way to get access to the HTML attributes of a WebDriverElement

>>> from splinter import Browser
>>> 
>>> browser = Browser('firefox')
>>> browser.visit('http://lovdtest.mcri.edu.au/individuals/00000143')
>>> imgs =  browser.find_by_tag('img')
[<splinter.driver.webdriver.WebDriverElement object at 0x1de2610>, <splinter.driver.webdriver.WebDriverElement object at 0x1de2690>]

I can get individual attributes by doing this (eg, imgs[0]['src'], imgs[0]['alt']) but there does not seem to be a keys() or items() method implemented in the WebDriverElement class.

imgs[0].__dict__ contains values that are unrelated to the HTML attributes. {'action_chains': , '_element': , 'parent': }

What can I do to get a dictionary with information on the HTML attributes of imgs[0]?

{"src": "gfx/header1.png", "alt": "Logo", "width": "172",
"height": "31", "id": "site_logo", "style": 
"margin-top : 5px; cursor : pointer;"}

Upvotes: 1

Views: 2394

Answers (2)

Nick Noble
Nick Noble

Reputation: 1

From the Splinter API Docs:

Any element in the page can be represented as an instance of ElementAPI. Once you have an instance, you can easily access attributes like a dict:

element = browser.find_by_id("link-logo").first
assert element['href'] == 'https://splinter.readthedocs.io'

This might be a new addition since the question was originally asked!

Upvotes: 0

supermice
supermice

Reputation: 1

You can try this

imgs =  browser.find_by_tag('img')
for image in imgs:
    image._element.get_attribute('id')

Upvotes: 0

Related Questions