Home > front end >  Get input values using Selenium in Python outputs None
Get input values using Selenium in Python outputs None

Time:09-07

My Item:

<input id="a0"  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

CodePudding user response:

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)

CodePudding user response:

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).

  • Related