Alex Dana
Alex Dana

Reputation: 1307

Download background Image with CSS selector - Selenium Python

I'm trying to extract all the pictures available on this website page with Python and Selenium

For this, i'm using the CSS selector, with the following code :

try :
    Pictures = self.browser.find_elements_by_css_selector('background-image')
except :
    Pictures = None

for picture in Pictures :
    print(picture.get_attribute("textContent"))

However, nothing happens : no message error, no blanks... just nothing.

My final target is to be able to download these pictures ; how can I do ?

Edit 1 :

enter image description here

Upvotes: 0

Views: 1143

Answers (2)

SIM
SIM

Reputation: 22440

Try the following to get the images from that page. Turn out that they are available in the page source, so you don't need to make use of selenium to grab the images. You can use requests module which is way faster than selenium. Given that this is how you can go:

import re
import requests
from bs4 import BeautifulSoup

link = "https://www.sezane.com/fr/e-shop/collection-printemps-robes-robes-courtes"

res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("[id='cms_sortable'] .free-product__img"):
    match_image = re.search(r"url\('(.*?)'",item.get("style"))
    if match_image:
        print(match_image.group(1))

Output are like:

https://media.sezane.com/image/upload/q_auto:best/v1586276035/website/cms/product/imh92y33wziscjtz1mta.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585924567/website/cms/product/c7umxdbyyqocwc5gzghy.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585924561/website/cms/product/jfdjdxqo5ek3oeziggxc.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585931708/website/cms/product/qhmxfoo95ftlbzsiplid.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585907671/website/cms/product/ng7vbbd39cnooqzqgzgt.jpg

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

please try below solution, in your code csss selector was incorrect ::

 driver.maximize_window()
 driver.get('https://www.sezane.com/fr/e-shop/collection-printemps-robes-robes-courtes')   
 elements = WebDriverWait(driver, 15).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@id,'_divVisualPrimary')]")))

for element in elements:
   element=element.value_of_css_property("background-image")
   print(re.split('[()]',element)[1])

Note : please add below imports to your solution

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

enter image description here

Upvotes: 0

Related Questions