Search code examples
pythonseleniumselenium-webdriver

python selenium driver.quit() won't terminate the program midway


The following section of the code suppose to terminate(stop) the program when except occurs, with driver.quit(). However, the program continues runs. what did I missed here?

try:
    driver.refresh()
    wait.until(ec.visibility_of_element_located(
        (By.XPATH, "//p[text()='Move']")))
    print("Waiting for Move")
    time.sleep (1)
except:
    print("All Move Completed")
    driver.quit()

Solution

  • You shouldn't call driver.quit() after driver.close() back to back.

    Remove driver.close() totally. Simply keeping driver.quit() will do the job.

    Your effective code block will be:

    while True:
        try:
            driver.refresh()
            wait.until(ec.visibility_of_element_located((By.XPATH, "//p[text()='Move']")))
            print("Waiting for Move")
            continue
        except TimeoutException:
            break
    print("All Move Completed")
    driver.quit()