Kevin Hale
Kevin Hale

Reputation: 128

Python Selenium "WebDriverElement' object has no attribute 'get_attribute'

I am using Selenium and Splinter to run my UI tests for my web application. I am generating a random id for the views on the page and would like to select the random ID for testing.

Here is the code I am using

from selenium import webdriver
from splinter import Browser
executable_path = {'executable_path':'./chromedriver.exe'}
browser = Browser('chrome', **executable_path)

data_view_id = browser.find_by_xpath('//ul[@class="nav"]').find_by_xpath('.//a[@href="#"]')[0].get_attribute("data-view-id")
# I am trying to get the id for the first item in the nav list and use it elsewhere
print(data_view_id)

This is the error I am receiving:

AttributeError: 'WebDriverElement' object has no attribute 'get_attribute'

I've looked at the 'readthedocs' page for WebElement and it has the 'get_attribute' value. I cannot find any documentation regarding WebDriverElements and need help accessing the WebElement instead

Upvotes: 1

Views: 3226

Answers (1)

Dean W.
Dean W.

Reputation: 642

That WebDriverElement is from Splinter, not Selenium.

In Splinter, you access attributes like a dict (see the Splinter docs)

data_view_id = browser.find_by_xpath('//ul[@class="nav"]').find_by_xpath('.//a[@href="#"]')[0]['data-view-id']

Or if you wanted to do it in Selenium:

browser.find_element_by_xpath('//ul[@class="nav"]').find_element_by_xpath('.//a[@href="#"]').get_attribute("data-view-id")

Upvotes: 5

Related Questions