0

I am getting the following error when trying to parse the last page of my loop and exit the loop once the next (») button no longer exists: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: »

Code:

while True:
    prices = driver.find_element_by_id('showAllGraphsButton')
    prices.click()
    time.sleep(6)
    s_container = driver.find_element_by_id('stockContainer')
    stocks = WebDriverWait(s_container, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "imageRow")))
    for stock in stocks:
        name = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "auto-ellipsis"))).text
        stock_name.append(name)
        percentage = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "return-value"))).text
        stock_percentage.append(percentage)
        price = WebDriverWait(stock, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "last-price"))).text
        stock_price.append(price)

        if len(driver.find_element_by_link_text('»').text) > 0:
            driver.find_element_by_link_text('»').click()
            time.sleep(6)
        else:
            break

driver.quit()

Please may I have some guidance on how to successfully exit the loop when the element no longer exists. Thank you.

1
  • You can use try...except block to break the loop without error. Did you try that? Commented Oct 25, 2020 at 11:28

1 Answer 1

1

Try this: put "try" and "except" function in your code. With them the code don't return error, but it simply stop. If you want, you can repeat the loop in case of error, you can only put break at finish of try. while True: try: #some code break except: #some code

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

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.