Home > Net >  Selenium - ".text" Unresolved attribute reference 'text' for class 'list�
Selenium - ".text" Unresolved attribute reference 'text' for class 'list�

Time:08-03

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.

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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")])
    
  • Related