问题
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.
回答1:
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()
回答2:
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 branchfinally
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!
来源:https://stackoverflow.com/questions/59811236/start-from-the-beginning-using-while-loop-on-connection-times-out-using-selenium