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

How to get value of nested class with Selenium Python

Hello i have problem i don’t know how to get value of offer__name and offer_price.

<div class="offer">
  <div class="offer__name">
      <em> item1 </em>
      <em> description of item1 </em>
  </div>
  <div class="offer__price">500</div>
</div>

<div class="offer">
  <div class="offer__name">
      <em> item2 </em>
      <em> description of item2 </em>
  </div>
  <div class="offer__price">200</div>
</div>

<div class="offer">
  <div class="offer__name">
      <em> item3 </em>
      <em> description of item3 </em>
  </div>
  <div class="offer__price">100</div>
</div>

i try with

objects = driver.findElement(By.xpath("//*[@class='offer']")));
for offer in objects:
    ...

but for-loop get me notice that object can’t be iterated, can you help me?

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

I just want value like:
item1 description of item1 500, item2 description of item2 200, item3 description of item3 100,

>Solution :

It is simple just grab the elements in list. You need find_elements_*() method to get list of elements where as you are using – find_element_*()
code for your reference –

lst_ele = driver.find_elements_by_xpath(".//div[@class='offer']")
    for i in lst_ele:
        print(i.text)

Output –

enter image description here

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