从一开始就使用Selenium和Python在连接超时时使用while循环 [英] Start from the beginning using while loop on connection times out using Selenium and Python
本文介绍了从一开始就使用Selenium和Python在连接超时时使用while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
推荐答案
您似乎很完美.为了演示当连接再次超时时从头开始,并进行永无止境的循环直到连接成功" ,这是一个小程序,执行以下操作:
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:
- 打开网址 https://www.google.com/
- 找出元素
By.NAME的"q"
- 清除字段
- 发送字符序列名称
- 尝试单击元素
find_element_by_tag_name('button')
- 失败并使用
continue
继续重试. -
代码块:
- 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
.
.
.
您可以遵循类似的逻辑,并且您的有效代码块将是:
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()
这篇关于从一开始就使用Selenium和Python在连接超时时使用while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文