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()