Home > OS >  Finding element from Inspet Chrome
Finding element from Inspet Chrome

Time:11-18

SO basically Im trying to find how many brooches are on this site: https://www.swarovski.com/en-RO/c-0107/Categories/Jewelry/Brooches/

and my code is this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.swarovski.com/en-RO/c-0107/Categories/Jewelry/Brooches/")

wait = WebDriverWait(driver, 10)
all_products = driver.find_elements(By.XPATH, '/html/body/main/div[2]/section[2]/div[2]/div/div/div/div[1]/div/a/div[2]/p/span[1]')
print(f"Number of products: {len(all_products)}")

for product in all_products:
    product_name = product.find_elements(By.XPATH, '/html/body/main/div[2]/section[2]/div[2]/div/div/div/div[1]/div/a/div[2]/p/span[1]')
    product_name = product_name.text
    print(product_name)

Output: AttributeError: 'list' object has no attribute 'text'

Any solutions would be much appreciated :)

I tried to change it to

print(product_name.get_attribute("innerHTML"))

Didn't work because now it shows: AttributeError: 'list' object has no attribute 'get_attribute'. Did you mean: 'getattribute'?

I tried to change it to CSS_SELECTOR, same error AttributeError: 'list' object has no attribute 'text'

CodePudding user response:

find_elements method returns a list of web elements.
Your mistake is with this line:

product_name = product.find_elements(By.XPATH, '/html/body/main/div[2]/section[2]/div[2]/div/div/div/div[1]/div/a/div[2]/p/span[1]')

You need to use find_element method here, not find_elements.
The following code will not give the error you mentioned, while your code still will not work correctly...

for product in all_products:
    product_name = product.find_element(By.XPATH, '/html/body/main/div[2]/section[2]/div[2]/div/div/div/div[1]/div/a/div[2]/p/span[1]')
    product_name = product_name.text
    print(product_name)

To make your code working I used WebDriverWait expected_conditions explicit waits to wait for elements to appear on the page. Then I get the amount of the products on the page and extracted the title name from each product.
This is the code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)


url = "https://www.swarovski.com/en-RO/c-0107/Categories/Jewelry/Brooches/"
driver.get(url)


items = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.swa-product-tile-plp")))
print(str(len(items))   " products found")
for item in items:
    name = item.find_element(By.CSS_SELECTOR, ".swa-product-sans--name").text
    print(name)

The output is:

6 products found
Eternal Flower pendant and brooch
Stella brooch
Eternal Flower pendant and brooch
Curiosa brooch
Dellium Brooch
Stella brooch
  • Related