Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through elements get repeating result on Selenium on Python

I'm having an issue that I'm struggling to understand. I have a HTML with a ul list with a bunch of li items, and within those li items they have elements that I would like to scrape using Selenium on Python. The website makeup is as follows

<ul id="results">
    <li>
        <div class="name">John</div>
        <div class="age">23</div>
    </li>
    <li>
        <div class="name">Bob</div>
        <div class="age">39</div>
    </li>
    ..... #more li
</ul>

So this seems like a pretty simple problem where we just save the li elements as a variable, and iterate through each list to save the information. The problem is that no matter what I do, my results always gives back the first list item over and over again. It will loop through the correct number of elements, but always refer back to the first one. So if I do the following

results = driver.find_elements_by_xpath("""//*[@id="results"]/li""")
for result in results:
    name = result.find_element_by_xpath("""//*[@class="name"]""").text
    print(name)

Now if there is 10 li elements in this particular case, the name "John" will just print out 10 times rather than updating based on the iterated list.

like image 779
Ryan N. Avatar asked Oct 19 '25 05:10

Ryan N.


1 Answers

Your XPath for the 2nd search is incorrect. It must begin with .. Otherwise, it will start searching from the top. That's the reason why it always find the first item. See my example below.

results = driver.find_elements_by_xpath('//*[@id="results"]/li')
for result in results:
    name = result.find_element_by_xpath('.//*[@class="name"]').text
    print(name)
like image 157
Buaban Avatar answered Oct 21 '25 17:10

Buaban



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!