简体   繁体   中英

Start from the beginning using while loop on connection times out using Selenium and Python

from selenium import webdriver
import time

browser = webdriver.Chrome('C:/Users/acer/Desktop/chromedriver')
browser.get('website')

def user():
    while True:
        time.sleep(1)
        try:
            browser.find_element_by_id('q').send_keys('name') #Type in name
            browser.find_element_by_tag_name('button').click()  #Click "verify"

        finally:
            browser.find_element_by_tag_name('button').click()  #When connection times out, click "try again"
user()      #When connection times out again run "while loop" from the begining

I want to start from the beginning when the connection times out again and make a never ending loop until connection is successful.

So you're almost there. You need to use try/except rather than try/finally.

Selenium will raise a timeout exception. except and finally are both used to when dealing with exceptions:

  • except when python raises an exception the program will exit unless you catch the exception using an except branch
  • finally if an exception is raised, any code in the finally branch is executed before the program exits.

If you use an except branch then the program will just loop around again. Make sure that you catch the specific timeout exception in the except branch otherwise your program will slow all errors!

Seems you were near perfect. To demonstrate "to start from the beginning when the connection times out again and make a never ending loop until connection is successful" here is a small program which does the following:

  • Opens the url https://www.google.com/
  • Finds out the element By.NAME, "q"
  • Clears the field
  • Sends the character sequence name
  • Attempts to click on the element find_element_by_tag_name('button')
  • Fails and using continue keeps on retrying.
  • Code Block:

     from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, WebDriverException options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) browser = webdriver.Chrome(options=options, executable_path=r'C:\\WebDrivers\\chromedriver.exe') browser.get('https://www.google.com/') def user(): while True: print("Starting while loop") try: element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.NAME, "q"))) element.clear() #clear the previous text element.send_keys('name') #Type in name browser.find_element_by_tag_name('button').click() except (WebDriverException, TimeoutException): print("Go to while loop from the begining") continue user()
  • Console Output:

     Starting while loop Go to while loop from the begining Starting while loop Go to while loop from the begining Starting while loop Go to while loop from the begining . . .

This usecase

You can follow similar logic and your effective code block will be:

from selenium import webdriver
import time

browser = webdriver.Chrome('C:/Users/acer/Desktop/chromedriver')
browser.get('website')

def user():
    while True:
    time.sleep(1)
    try:
        browser.find_element_by_id('q').send_keys('name') #Type in name
        browser.find_element_by_tag_name('button').click()  #Click "verify"

    except WebDriverException:
        continue #When connection times out again run "while loop" from the begining
user()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM