1

I would like to iterate through the below elements in Python using Selenium webdriver.

<ul class="skills-section">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Ear Surgery">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Healthcare">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Hospitals">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Surgery">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Medical Education">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Pediatrics">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Treatment">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Public Health">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Patient Safety">
  <li class="endorse-item has-endorsements " data-endorsed-item-name="Emergency Medicine">
</ul>

What I tried?

skillsSection = a.find_element_by_xpath("//ul[contains(@class, 'skills-section')]")
skillsList = skillsSection.find_elements_by_tag_name("li")
for skill in skillsList:
    print skill.find_element_by_xpath("//span[contains(@class,endorse-item-name')]/a").text  

But the problem is, it is always printing the first element value. The element is not incrementing.

2 Answers 2

1

What you'll want to do is find the tag 'ul' with class='skills-section' and then iterate over the children.

skillsSection = a.find_element_by_xpath("//ul[contains(@class, 'skills-section')]")
for child in skillsSection.find_elements_by_xpath(".//*"):
    ...

See this answer too for more details: Selenium Python get all children elements

Sign up to request clarification or add additional context in comments.

Comments

0

I've been dealing with a similar issue recently where I could only print the first item in a list. I discovered a solution that requires multiple lines, and avoids using xpaths. For your code, you may want to replace:

print skill.find_element_by_xpath("//span[contains(@class,'endorse-item-name')]/a").text  

with something like:

a1 = skill.find_element_by_class_name('endorse-item-name')
a2 = a1.find_element_by_tag_name('a')
print a2.text

Not as elegant as one line with xpath, but it (should) work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.