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

Question:

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:

<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.

Asked By: supras

||

Answers:

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")
Answered By: AbiSaran
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.