Selenium 'WebElement' object has no attribute 'Get_Attribute'
Asked Answered
A

5

23

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.

Assembler answered 7/4, 2016 at 12:55 Comment(0)
D
45

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
Debauchery answered 7/4, 2016 at 13:7 Comment(0)
C
3

For python with input-field is like:

nowText = driver.find_element_by_id("source").get_attribute("value")
print(nowText)
Counterreply answered 8/9, 2018 at 12:27 Comment(0)
P
1
src = driver.find_element_by_css_selector("img").get_attribute("src")
Palumbo answered 9/3, 2019 at 15:58 Comment(0)
S
0

For the latest Selenium version the method find_element_by_id is deprecated, find_element should be used and the same happens to get_attribute instead of getAttribute.

Spinner answered 10/6, 2023 at 20:53 Comment(0)
E
-1

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()
Elkin answered 10/12, 2022 at 15:10 Comment(1)
The question is about attribute method, not .text methodSpinner

© 2022 - 2024 — McMap. All rights reserved.