0

When we say "inspect" Chrome, it finds the jpg file and let's copy the xpath. When I try to import it in python, it comes as base64. When I decode the code, a small white image comes up. A blank image appears.

import base64
from io import BytesIO
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

def base64_to_image(base64_string):
    image_data = base64.b64decode(base64_string)
    image = Image.open(BytesIO(image_data))
    return image

options = Options()
options.add_argument("--disable-notifications")
options.add_experimental_option("excludeSwitches", ["enable-automation"]) #blutoot hata giderme
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.implicitly_wait(5)
driver.set_window_size(800, 200)
driver.get('https://www.migros.com.tr/sut-kahvaltilik-c-4?sayfa=1')
elems = driver.find_element(By.XPATH,"/html/body/sm-root/div/main/sm-product/article/sm-list/div/div[4]/div[2]/div[4]/sm-list-page-item[1]/mat-card/div[1]/fe-product-image/a/img")

#<img _ngcontent-ohx-c159="" felazyload="" alt="Yumurtacım 15'li L Boy Yumurta (63-72 G)" src="https://images.migrosone.com/sanalmarket/product/20001975/20001975-cdebd9.jpg" class="ng-star-inserted">
print(elems.get_attribute("src"))

# Base64 kodunu buraya girin
base64_string = elems.get_attribute("src").replace("data:image/png;base64,","")
decoded_image = base64_to_image(base64_string)
# Resmi göstermek için
decoded_image.show()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
hepburda
  • 1
  • 1

1 Answers1

0

To print the value of the src attribute you need to use get_attribute() method and inducing WebDriverWait for the visibility_of_element_located() you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.migros.com.tr/sut-kahvaltilik-c-4?sayfa=1")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.accept-all"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.product-cards sm-list-page-item:first-of-type img"))).get_attribute("src"))
    
  • Using XPATH:

    driver.get("https://www.migros.com.tr/sut-kahvaltilik-c-4?sayfa=1")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.accept-all"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'product-cards')]//following::sm-list-page-item[1]//img"))).get_attribute("src"))
    
  • Console output:

    https://images.migrosone.com/sanalmarket/product/20001975/20001975-cdebd9.jpg
    
  • Note : You have to add the following imports :

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

You can find a relevant discussion in Python Selenium - get href value

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for the codes you provided, but the result is still not found. finds a blank small white image. Could it be related to the python version? I can buy from other sites, only this site is problematic, what's the problem? – hepburda Jun 19 '23 at 08:29
  • @hepburda Your basic requirement was to extract the `src` attribute, right? Doesn't the answer does that? – undetected Selenium Jun 19 '23 at 08:34
  • yes it doesn't find it, it finds the following: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAKCAYAAABrGwT5AAAAF0lEQVR42mN89uzxfwYyAeOo5lHNhAAA7jIk17T4Y6wAAAAASUVORK5CYII= when i try to base64 decode it gives a blank, small white image – hepburda Jun 19 '23 at 08:43