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

Python Selenium, find certain elements under a certain element

<section id='browse-search'>
  <div>
      <div>
          <div>
                <div class='product-pod'>
                <div class='product-pod>'>
          </div>
      </div>
  </div>
</section>
<div class='product-pod>'>
<div class='product-pod>'>

I have a webpage like this structure. and I need a cleaner way to locate elements with class=’product-pod’. driver.find_elements(By.XPATH,"div[@class='product-pod']") will not work, because there are a few matched elements outside the section element.

Please advise what is the most appropriate way to locate those elements.

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

>Solution :

With what you have provided, this strategy could be built:

if the extra > that you provided is a typo, then:

For first div element:

driver.find_element(By.XPATH, "(//section[@id='browse-search']//div[@class='product-pod'])[1]")

For second div element:

driver.find_element(By.XPATH, "(//section[@id='browse-search']//div[@class='product-pod'])[2]")

If the > is not a typo, then the structure changes, and the below strategies would work:

For main div element:

driver.find_element(By.XPATH, "//section[@id='browse-search']//div[@class='product-pod']")

For inner div element:

driver.find_element(By.XPATH, "//section[@id='browse-search']//div[@class='product-pod']/div")
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