Python – Locate attributes with Selenium

Issue

I would like to extract the link , the picture link, the
sponsored flag and the rating stars from this link https://www.skroutz.gr/c/40/kinhta-thlefwna.html.
I manage to extract the rest but for those I only can extract them with the .get_attribute() function which i can’t use it because I want to store them in a list.

So can anyone help me how to locate these attributes ?

Thanks in advance.

Solution

The below example will help you to locate attributes with Selenium

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

lst=[]

website = 'https://www.skroutz.gr/c/40/kinhta-thlefwna.html'
driver.get(website)
time.sleep(2)
cards = driver.find_elements(By.XPATH,'//*[@id="sku-list"]/li') 
for card in cards:
    link= card.find_element(By.XPATH,'.//*[@class="js-sku-link pic"]').get_attribute('href')
    img =  card.find_element(By.XPATH,'.//*[@class="js-sku-link pic"]/img').get_attribute('src')
    star_rating = card.find_element(By.XPATH,'.//*[@class="rating-wrapper"]/span').text
    lst.append({
        'link':link,
        'img':img,
        'star_rating':star_rating})
print(lst)
    

Output:

[{'link': 'https://www.skroutz.gr/s/30475704/Sony-Xperia-1-III-5G-Single-SIM-12GB-256GB-Frosted-Black.html?from=featured&product_id=85046151', 'img': 'https://c.scdn.gr/images/sku_main_images/030475/30475704/large_20211214103218_sony_xperia_1_iii_5g_12gb_256gb_single_sim_frosted_black.jpeg', 'star_rating': '4.8'}, {'link': 'https://www.skroutz.gr/s/32430220/Sony-Xperia-5-III-5G-Dual-SIM-8GB-128GB-%CE%9C%CE%B1%CF%8D%CF%81%CE%BF.html?from=featured&product_id=95502499', 'img': 'https://a.scdn.gr/images/sku_main_images/032430/32430220/large_20211213172910_sony_xperia_5_iii_5g_8gb_128gb_mayro.jpeg', 'star_rating': '4.7'}, {'link': 'https://www.skroutz.gr/s/29156696/Sony-Xperia-10-III-5G-Dual-SIM-6GB-128GB-White.html?from=featured&product_id=77953206', 'img': 'https://b.scdn.gr/images/sku_main_images/029156/29156696/large_20211214102839_sony_xperia_10_iii_5g_6gb_128gb_white.jpeg', 'star_rating': '4.3'}, {'link': 'https://www.skroutz.gr/s/28946324/Sony-Xperia-10-III-5G-Dual-SIM-6GB-128GB-Black.html?from=featured&product_id=77947125', 'img': 'https://b.scdn.gr/images/sku_main_images/028946/28946324/large_20210518104628_sony_xperia_10_iii_128gb_black.jpeg', 'star_rating': '4.0'}, {'link': 'https://www.skroutz.gr/s/30662995/Samsung-Galaxy-A52s-5G-Dual-SIM-6GB-128GB-Awesome-Black.html', 'img': 'https://c.scdn.gr/images/sku_main_images/030662/30662995/large_20210830101603_samsung_galaxy_a52s_128gb_awesome_black.jpeg', 'star_rating': '4.4'}, {'link': 'https://www.skroutz.gr/s/20060269/Apple-iPhone-11-4GB-64GB-Black.html', 'img': 'https://c.scdn.gr/images/sku_main_images/020060/20060269/large_20190916100719_apple_iphone_11_64gb.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/31694271/Realme-8i-Dual-SIM-4GB-128GB-Space-Black.html', 'img': 'https://d.scdn.gr/images/sku_main_images/031694/31694271/large_20211014134513_realme_8i_128gb_space_black.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/32504283/Realme-GT-Neo-2-5G-Dual-SIM-8GB-128GB-Neo-Black.html', 'img': 'https://b.scdn.gr/images/sku_main_images/032504/32504283/large_20211122115729_realme_gt_neo_2_5g_8gb_128gb_neo_black.jpeg', 'star_rating': '4.7'}, 
{'link': 'https://www.skroutz.gr/s/31105224/Apple-iPhone-13-Pro-Max-5G-6GB-128GB-Graphite.html', 'img': 'https://a.scdn.gr/images/sku_main_images/031105/31105224/large_20210920153335_apple_iphone_13_pro_max_128gb_graphite.jpeg', 'star_rating': '4.5'}, {'link': 'https://www.skroutz.gr/s/31694187/Realme-8i-Dual-SIM-4GB-64GB-Space-Black.html', 'img': 'https://d.scdn.gr/images/sku_main_images/031694/31694187/large_20211014134219_realme_8i_64gb_space_black.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/30717392/Samsung-Galaxy-A52s-5G-Dual-SIM-6GB-128GB-Awesome-White.html', 'img': 'https://d.scdn.gr/images/sku_main_images/030717/30717392/large_20210901102946_samsung_galaxy_a52s_128gb_awesome_white.jpeg', 'star_rating': '4.4'}

… so on

Answered By – F.Hoque

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published