Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting links and background image from a certain div's using – Selenium in Python

I’m trying to get all the links and background images of the links inside a specific div But i can’t seem to get them.

I’m trying to get the links and the background images inside paint_wrap div not paint_color.

HTML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div id="timer" style="display:inline-block">
  <div class="paint_wrap">
    <div class="paint_color">
      <img src="http://link.com/img/6EAwxqqt6J7aKcn6B6pO.gif" class="pick" width="30" height="30" border="0" alt="">
      <img src="http://link.com/img/arrow.gif" class="arrow" width="27" height="15" border="0" alt="">
    </div>

    <a href="tx.php?id=91760&amp;a=f899139df5e105939643&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="evJ3Ic2F5rYtXH4sPFKN" alt=""></a>
    <a href="tx.php?id=91760&amp;a=3644a684f98ea8fe223c&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="Rrp84pHupovp4G9R8DfJ" alt=""></a>
    <a href="tx.php?id=91760&amp;a=94f6d7e04a4d45203530&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="z071fnIXwHK7MDd7M58t" alt=""></a>
    <a href="tx.php?id=91760&amp;a=18d8042386b79e2c279f&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="UT94OVZ614YGvkFIuh4P" alt=""></a>
    <a href="tx.php?id=91760&amp;a=cee631121c2ec9232f3a&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="ZC7TO9wC576wN0Q9017j" alt=""></a>
    <a href="tx.php?id=91760&amp;a=d490d7b4576290fa60eb&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top" class="G2e7xW9ii7Lp40kH140X" alt=""></a>
    <a href="http://www.other-link.com/?5974792" target="_blank" class="open"><img src="http://link.com/tx/o.gif" width="15" height="15" border="0" alt=""></a>
  </div>
</div>

WHAT I HAVE TRIED:

WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='timer']/div/a"))) # Wait is fine
elements = self.driver.find_element(By.XPATH, "//*[@id='timer']/div/a")
for element in elements:
   print(element.get_attribute("href"))
   print(element.value_of_css_property("background-image"))  

I have also tried it with using css selector.

THE ERROR I GET:
‘WebElement’ object is not iterable

I’m hoping maybe someone could point me in the right direction.

>Solution :

You have to use ‘find_elements’ in the below line instead of ‘find_element’:

elements = self.driver.find_elements(By.XPATH, "//*[@id='timer']/div/a")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading