Selenium 'WebElement' object has no attribute 'Get_Attribute'

Question:

I’m using Selenium webdriver (chrome) with Python, I’m trying to get the href from all the links on the webpage. When I try the following:

items = driver.find_elements_by_tag_name("a")
print items

for item in items:
    href = item.Get_Attribute('href')
    print href

It manages to get all the links, but on get_attribute I get an error:

‘WebElement’ object has no attribute ‘Get_Attribute’

Though everywhere I looked it seems like it should work.

Asked By: Sapir

||

Answers:

The “Get_Attribute” property doesn’t exist, but the “get_attribute” property does:

items = driver.find_elements_by_tag_name("a")
print items

for item in items:
    href = item.get_attribute('href')
    print href
Answered By: Florent B.

For python with input-field is like:

nowText = driver.find_element_by_id("source").get_attribute("value")
print(nowText)
Answered By: YHS
src = driver.find_element_by_css_selector("img").get_attribute("src")
Answered By: Rkmr039

in the latest version
find_element_by_id is deprecated, find_element should be used

# Import Selenium's WebDriver module
from selenium import webdriver

# Create a WebDriver object
driver = webdriver. Chrome()

# Use the get method of the WebDriver object to open the specified URL
driver.get("https://www.example.com")

# Use the find_element method to get the specified element
element = driver.find_element(By.ID, "input-id")

# Get the text in the element
text = element.text
print(text)

# Close the WebDriver object
driver. quit()

Answered By: Ken Chang