1

https://www.selenium.dev/documentation/webdriver/elements/information/#text-content

According to Selenium, it said:

# Navigate to url
driver.get("https://www.example.com")

# Retrieves the text of the element
text = driver.find_element(By.CSS_SELECTOR, "h1").text

But when I follow this rule, it shows Unresolved attribute reference 'text' for class 'list'.

vegetables_search = driver.find_elements(By.CLASS_NAME, "info-wrapper").text

I don't know what's going on. When I don't put ".text". Everything gets fine and shows:

Product_Name:[<selenium.webdriver.remote.webelement.WebElement (session="a0aedd6c6d1e0371c2781854606db200", element="6c140c85-242b-45fe-bf45-5217d7d979c1")>]
Original_Price:[<selenium.webdriver.remote.webelement.WebElement (session="a0aedd6c6d1e0371c2781854606db200", element="2c646052-c91e-41a2-b7f8-125240dd449b")>]
Product_Price:[<selenium.webdriver.remote.webelement.WebElement (session="a0aedd6c6d1e0371c2781854606db200", element="2edec63b-56e2-49e7-963f-e96d41beabcd")>]

This is my code:

#data setting
vegetables_search = driver.find_elements(By.CLASS_NAME, "info-wrapper")

for search in vegetables_search:
 Product_name = search.find_elements(By.TAG_NAME, "h4")
 Original_price = search.find_elements(By.CLASS_NAME, "promotional")
 Product_price = search.find_elements(By.CLASS_NAME, "price")

 print(f'Product_Name:{Product_name}\nOriginal_Price:{Original_price}\nProduct_Price:{Product_price}')
 print(" ")

Please help me to solve this problem. I want to change "selenium.webdriver.remote.webelement.WebElement" into text. Thankyou.

2
  • 1
    driver.find_elements(By.CLASS_NAME, "info-wrapper") returns a list, which doesn't have the text attribute. Commented Aug 3, 2022 at 9:49
  • what's the actual url? Commented Aug 3, 2022 at 10:08

3 Answers 3

1

find_elements method returns a list of WebElement objects while find_element method returns a single WebElement object.
text method can be applied on a single WebElement object to extract it text content.
text method can not be applied on a list of objects.
You should use find_element there, as following:

#data setting
vegetables_search = driver.find_elements(By.CLASS_NAME, "info-wrapper")

for search in vegetables_search:
 Product_name = search.find_element(By.TAG_NAME, "h4").text
 Original_price = search.find_element(By.CLASS_NAME, "promotional").text
 Product_price = search.find_element(By.CLASS_NAME, "price").text

 print(f'Product_Name:{Product_name}\nOriginal_Price:{Original_price}\nProduct_Price:{Product_price}')
 print(" ")

Or in case these are expected to be a list of elements - iterate over the lists and apply text method on each element in the list.

Sign up to request clarification or add additional context in comments.

3 Comments

It works, thanks. But I wonder how can I print readable texts if I want to search in "elements".
"stackoverflow.com/questions/67170094/selenium-iterate-same-url" Just like this scenario. This OP ran the same result(links) because of using "find_element".
@hectorcchan this your question about printing a readable texts is not clear enough. Also, now it's another, new question. I think we answered the question you asked here. So, please accept the answer here and then ask a new, separate question about printing texts problem.
0

Try using:

vegetables_search = driver.find_elements(By.CLASS_NAME, "info-wrapper")
print(vegetables_search.text)

Or if it doesn't bring everything then,

use vegetables_search.get_attribute("textContent")

Comments

0

This error message...

Unresolved attribute reference 'text' for class 'list'

...implies that there is no attribute as text for list type objects.

driver.find_elements* returns a list, where as text is a WebElement attribute. Hence you see the error.


Solution

To print the texts from the (By.CLASS_NAME, "info-wrapper") classes, you can use list comprehension and you can use either of the following locator strategies:

  • Using CLASS_NAME:

    print([my_elem.text for my_elem in driver.find_elements(By.CLASS_NAME, "info-wrapper")])
    

1 Comment

Isn't the original problem because the OP was accessing the text attribute on a list result?

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.