Home > Back-end >  Using Selenium to get image src inside of a link
Using Selenium to get image src inside of a link

Time:05-02

I'm trying to get some info from this page using Selenium:

enter image description here

Here's the page inspector - the src tag I'm trying to get is circled:

enter image description here

I need to access the src without referencing any explicit div names, so what I want to do is find the href instance which contains the image, and then search for the image inside of the a tag.

Here's my code, which fails to retrieve anything (printing find_src prints nothing) :

find_src = driver.find_element_by_css_selector('a[href^="https://www.bandsintown.com/a/"]//img').get_attribute("src")

What am I doing wrong here?

CodePudding user response:

Please retry by replacing your css selector expression with this one: "a[href^='https://www.bandsintown.com/a/'] img". So, you rectified code will be:

find_src = driver.find_element_by_css_selector("a[href^='https://www.bandsintown.com/a/'] img").get_attribute("src")

If we want to select a descendant element using CSS selectors, space " " is used but in your CSS selector expression, "//" is there. For more information, kindly refer this link: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

  • Related