0

How would I go about running a loop as long as a specific div-element is present on a website?

I need the parser to go through a number of pages and click next for each page - the last page is empty and thus lacks the aforementioned div-element, so I figured running this within a while-loop would be a good approach. I'm however not sure how I should make this properly.

What I currently have, though non-functioning, is:

test = driver.find_element_by_class_name("divClassName").size()

, and subsequently checking whether its size is while(test != 0).

Any suggestions would be much appreciated!

4
  • could you show what you got so far? stackoverflow.com/help/how-to-ask Commented Mar 23, 2017 at 11:51
  • Hi Richy, my bad, I've updated my question a bit - hopefully with additional clarity. I initially did not want to include things as it is not working, but having read the page I've updated accordingly. Commented Mar 23, 2017 at 12:03
  • thanks for the update, but would be even better if you could show us still more. How does the loop look? Maybe the solution could be something like while true: if not test: break Commented Mar 23, 2017 at 12:06
  • The way I've approached this has been to first code the parsing for one page, subsequently going through each page. I've now taken all this - which works on its own - and placed it within the aforementioned while-loop to ensure it only runs till the last, empty page. Commented Mar 23, 2017 at 12:12

1 Answer 1

1

Try to implement below approach:

from selenium.common.exceptions import NoSuchElementException    

while True:
    try:
        driver.find_element_by_class_name("divClassName")
        # do what you need
        # click "Next" button
    except NoSuchElementException:
        break

With above code you'll be able to get new page and perform actions if <div class="divClassName"> is found, and stops otherwise

Also note that size is a property of webelement, but not a method. You should use it as webelement.size instead of webelement.size()

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

3 Comments

This works -- thank you very much! Also thanks for the note on size, much appreciated!
Instead of while, is there a way to just test it only once or twice?
Use for loop instead of while loop. E.g. for once in range(n): ... will run loop n times

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.