How to execute a while loop while an element is present on the page using selenium in python?

I am trying to create a while

-loop that works as long as a certain element is present on the website. What I currently have is by no means a solution I'm proud of, but it works. However, I am very grateful for some suggestions on how to change below:

def spider():
    url = 'https://stackoverflow.com/questions/ask'
    driver.get()

    while True:
        try:
            unique_element = driver.find_element_by_class_name("uniqueclass")

            do_something()

        except NoSuchElementException:
            print_data_to_file(entries)
            break

    do_something_else()

      

As you can see, the first thing I do in while

-loop is check for a unique element that is only present on pages that contain the data I'm interested in. Thus, when I reach a page without this information, I get NoSuchElementException

and break.

How can I achieve the above without doing while True

?

+3


source to share


1 answer


driver.find_elements

will not throw any errors. If the returned list is empty, it means there are no elements

def spider():
    url = 'https://stackoverflow.com/questions/ask'
    driver.get()

    while len(driver.find_elements_by_class_name("uniqueclass")) > 0:
        do_something()

do_something_else()

      



You can also use an explicit wait with the expected staleness_of condition , however you cannot execute do_something()

and this is used for short waiting periods.

+4


source







All Articles