Search code examples
python-3.xloopsselenium-webdrivergoto

Python3 goto assignment as infinite while-loop?


I am a python novice and did not understand how to assign a "goto" label in python3/selenium. I understood that there are none, but how can I write my script so that it does not fail to execute the infinite while-loop (here written in pseudo code)?:

while True:
    for-loop1
        something happens

    for-loop2
        something else is happening

    for-loop3
        even more stuff happens

    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li:not(.disabled)>a[data-page='next']"))).click() # checks for other page
        print("There is another page.")
        time.sleep( 2 ) # wait to load
    except NoSuchElementException:
        print("No further page exists. Bye, bye!")
        break

driver.quit()

I get the following error message:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li:not(.disabled)>a[data-page='next']"))).click() # checks for other page
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

Edit:

Problem solved with:

driver.find_element_by_css_selector("li:not(.disabled)>a[data-page='next']").click()

Solution

  • if I understand your problem correctly you don't need to use a "goto" at all. You can solve it by using a simple while loop wrapped around your for loops.

    while True:
        for things in loop1:
            do stuff
    
        for things in loop2:
            do more stuff
    
        if there_are_no_more_pages:
            break