Get input values using Selenium in Python outputs None

Question:

My Item:

<input id="a0" class="someclassname" size="55" placeholder="empty" value="scarping-test">

My Code:

items = driver.find_elements(By.XPATH,"//input[@id='a0']")
for item in items:
    href = item.get_attribute('href')
    print(href)

Output:

None

Expected:

scarping-test

Asked By: mathandlogic

||

Answers:

That input doesn’t have an href attribute. The attribute you’re looking for is called value.

So what you are looking for is:

item = driver.find_element(By.XPATH, "//input[@id='a0']").get_attribute('value')

I used find_element() instead of find_elements() since it has no sense to find for more than one element and loop through the list; the element with id='a0' can only be one (id is a unique identifier).

Answered By: wado

The attribute here is value, not href.
So, instead of

href = item.get_attribute('href')

Try

value = item.get_attribute('value')

So, the entire code will be:

items = driver.find_elements(By.XPATH,"//input[@id='a0']")
for item in items:
    value = item.get_attribute('value')
    print(value)
Answered By: Prophet
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.