Home > other >  Getting links and background image from a certain div's using - Selenium in Python
Getting links and background image from a certain div's using - Selenium in Python

Time:11-12

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 >
    <div >
      <img src="http://link.com/img/6EAwxqqt6J7aKcn6B6pO.gif"  width="30" height="30" border="0" alt="">
      <img src="http://link.com/img/arrow.gif"  width="27" height="15" border="0" alt="">
    </div>

    <a href="tx.php?id=91760&amp;a=f899139df5e105939643&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="tx.php?id=91760&amp;a=3644a684f98ea8fe223c&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="tx.php?id=91760&amp;a=94f6d7e04a4d45203530&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="tx.php?id=91760&amp;a=18d8042386b79e2c279f&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="tx.php?id=91760&amp;a=cee631121c2ec9232f3a&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="tx.php?id=91760&amp;a=d490d7b4576290fa60eb&amp;s=dba9df3b0d18635362fd70d1ec51f201" target="_top"  alt=""></a>
    <a href="http://www.other-link.com/?5974792" target="_blank" ><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.

CodePudding user response:

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