Reverse loop Selenium python

Asked

Viewed 95 times

1

I’m trying to do a reverse loop on my list of li in Lenium and it’s not rolling. I’ve tried one reversed in the list but error.

I want to loop the last one li until the first.

For example, in the list below:

<ul id="lista">
    <li>Eu nao</li>
    <li>Consigo</li>
    <li>Comecar de baixo pra cima</li>
</ul>

I would like the result to be bottom-up. The output would have to be this way:

Comecar de baixo pra cima
Consigo
Eu nao

Note: I can’t looping first into the elements by turning it into a list and then doing the reversed one. Because in my real case, the list of li is about 5000 items. However the most current ones are always the last ones, so there is no looping in 5000 items turning into list and then a second looping catch the last ones.

  • You can provide the link you want to do this to?

  • @Miguel edited with the example.

  • Your code? I have now run a test and it worked with reversed without problem

1 answer

1


I really don’t understand the problem that’s happening to you, it would help if you added your code, but here’s an example:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get('https://pt.wikipedia.org/wiki/Transportes_de_Lisboa')
lis = driver.find_elements_by_css_selector('#toc ul li')
for item in reversed(lis):
    print(item.text)

Note: that my driver (PhantomJS) might not be the same as yours, adjust to what you’re wearing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.