0

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

1
  • what have you tried? Show the code. Commented Nov 1, 2020 at 10:36

2 Answers 2

2

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"))
Sign up to request clarification or add additional context in comments.

Comments

0

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"))

3 Comments

You are right, this is what I wanted. but it doesn't work the way you suggest. it returns Message: no such element: Unable to locate element: {"method":"xpath","selector":"//img[src="moodle.technion.ac.il/theme/image.php/fordson/assign/1603222249/…"} (Session info: chrome=86.0.4240.111)
I guess this is because the image is deeper in the tree than just //img[src=....]
Use @src instead of src

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.