Dor Bitton
Dor Bitton

Reputation: 9

Using Selenium to find an image with a given src link and click

This is the html code of a website I want to scrape

<div>
  <div class="activityinstance">
    <a class="" onclick="" href="https://www.blablabla.com">
      <img src="http://www.blablabla.com/justapicture.jpg"  class="iconlarge activityicon" alt="" role="presentation" aria-hidden="true">
      <span class="instancename">title<span class="accesshide "> text
      </span>
      </span>
    </a>
  </div>
</div>
IMAGELINK = "http://www.blablabla.com/justapicture.jpg"

My aim is to find in particular page all of the hrefs that are associated with IMAGELINK using python. the picture from this url tend to be shown multiple times and I want to recieve all the links so I could click on them.

I tried to find elements by class name "a" to extract all of the links in the page, and that way if I could find their xPath I could just format "/img" and get attribute "src" from that element. But the problem is I haven't found a way to extract the xPath with given webdriver element.

NOTE: I don't have access to the Xpath of the element unless I write some function to generate it

Upvotes: 0

Views: 3442

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

I think he wanted the parent href with the img src which equals

imgs = driver.find_elements_by_xpath("//img[src='http://www.blablabla.com/justapicture.jpg']/parent::a")
for img in imgs:
    print(img.get_attribute("href"))

Upvotes: 0

Sushil
Sushil

Reputation: 5531

Find all elements with tag img and print the src attribute:

imgs = driver.find_elements_by_xpath("//img")
for img in imgs:
    print(img.get_attribute("src"))

Upvotes: 2

Related Questions