-2

I'm trying to extract the value of the alt attribute from the img tag.

enter image description here

I want to track title, but not able to track from img tag. I have written the below code to track.

try:
    Title = webdriver.find_element(By.XPATH, "(//div[@data-dyn='titleArtImage'])").text
    print("Title: " + Title)
except:
    TITLE = "missing title"
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
karthi
  • 23
  • 1
  • 4
  • Do you want to extract `@alt` value of `img` or to select `div` based on `@alt` value of child `img` node? – JaSON Aug 29 '22 at 19:13
  • @karthi Firstly, please post HTML in code format; image format would not help to debug generally. Secondly., is there title/text attribute for the img? The content in your image shows just `alt` and `srcset` attributes in the `img` tag, in which case it makes it almost impossible to tell you if you could extract the title from this line. YOu need to provide additional DOM content, or better the website link, if it is ok to share. – Anand Gautam Aug 29 '22 at 19:19
  • Also, `Title` and `TITLE` are two different variables unless your language is case insensitive. – choroba Aug 29 '22 at 19:27

1 Answers1

0

The value of the alt attribute of the descendent <img> holds the track title.

titletrack


Solution

To extract the value of the alt attribute you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-dyn='titleArtImage'] > img"))).get_attribute("alt"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-dyn='titleArtImage']/img"))).get_attribute("alt"))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352