4

I'm trying to copy text from comment on a website<span class="auto-link">yes</span> and my python code is

element=browser.find_elements_by_xpath('//span[@class="auto-link"][1]')
print(element.text)

but I keep on getting the 'list' object has no attribute 'text' error, I don't know what I'm doing wrong.

3

7 Answers 7

13

I'm using selenium in python. Try this code I hope this will work for you.

    element=browser.find_elements_by_xpath('//span[@class="auto-link"][1]')

    for value in element:
        print(value.text)
Sign up to request clarification or add additional context in comments.

Comments

3

I've never used Selenium, but based on the error and your response, the answer is pretty clear.

When you search for a class, there may be multiple matching elements, so it returns a list of all found matches. Even if you only have a single element with that class, it will still return a list for consistency.

Just grab the first element from the found elements:

elements = browser.find_elements_by_xpath('//span[@class="auto-link"][1]')
# ^ Renamed to reflect type better

print(elements[0].text)
#              ^ Grab the first element

Comments

2
# instead of driver.find_elements_by_xpath() use driver.find_element_by_xpath() to get the individual element of the table
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

# Google Chrome
driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://testautomationpractice.blogspot.com/") 

# Get Number of Rows
rows = len(driver.find_elements_by_xpath("//*[@id='HTML1']/div[1]/table/tbody/tr"))

# Get Number of Columns
columns = len(driver.find_elements_by_xpath("//*[@id='HTML1']/div[1]/table/tbody/tr[1]/th"))

print("Number of Rows:", rows)
print("Number of Columns:", columns)

# In web table index starts with 1 instead of 0 
for row in range(2, rows+1):
    for col in range(1,columns+1):
        value = driver.find_element_by_xpath("//*[@id='HTML1']/div[1]/table/tbody/tr["+str(row)+"]/td["+str(col)+"]").text
        print(value, end='      ')
    print()


time.sleep(5)

# Close the Browser
driver.close()

Comments

2

This will work when you are looking for more than one element and takes the first element that matches the xpath:

element=browser.find_elements_by_xpath('//span[@class="auto-link"][1]').get_attribute("innerHTML")
print(element)

This is when you are looking only for one:

element=browser.find_element_by_xpath('//span[@class="auto-link"]').get_attribute("innerHTML")
print(element)

The output:

>>>yes

Comments

1

First Write the xpath of span in which you are currently working and then add the index number in the last of xpath but within it like given below.

from selenium import webdriver`
driver = webdriver.Firefox()
driver.get("http://www.example.org")
element=browser.find_elements_by_xpath('//span[@class="auto-link"[1]').click()
print(element)

[1] is the index number of my value which i want to access.

Comments

1

dont use find_elements_by_xpath, but find_element_by_xpath

Comments

0

There are 10 elements by this selector, it prints all of them

roles = driver.find_elements_by_xpath("(//label[@class='container-checkmark disabled'])")

for x in range(len(roles)):
    print(roles[x].text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.