Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium和Python在连接超时时使用while循环从头开始_Python_Selenium_Google Chrome_While Loop_Try Catch - Fatal编程技术网

使用Selenium和Python在连接超时时使用while循环从头开始

使用Selenium和Python在连接超时时使用while循环从头开始,python,selenium,google-chrome,while-loop,try-catch,Python,Selenium,Google Chrome,While Loop,Try Catch,当连接再次超时时,我想从头开始,做一个永无止境的循环,直到连接成功。所以你就快到了。您需要使用try/except而不是try/finally Selenium将引发超时异常例外和最终在处理异常时都用于: except当python引发异常时,程序将退出,除非您使用except分支捕获异常 finally如果引发异常,则在程序退出之前执行finally分支中的任何代码 如果使用except分支,则程序将再次循环。确保在except分支中捕获特定的超时异常,否则程序将减缓所有错误 看来你近乎完

当连接再次超时时,我想从头开始,做一个永无止境的循环,直到连接成功。

所以你就快到了。您需要使用try/except而不是try/finally

Selenium将引发超时异常<代码>例外和
最终
在处理异常时都用于:

  • except
    当python引发异常时,程序将退出,除非您使用except分支捕获异常
  • finally
    如果引发异常,则在程序退出之前执行finally分支中的任何代码

如果使用except分支,则程序将再次循环。确保在except分支中捕获特定的超时异常,否则程序将减缓所有错误

看来你近乎完美。为了演示“在连接再次超时时从头开始,并在连接成功之前进行永无止境的循环”,这里有一个执行以下操作的小程序:

  • 打开url
  • 通过.NAME“q”查找元素
  • 清除字段
  • 发送字符序列名称
  • 尝试单击元素
    通过标记名称(“按钮”)查找元素
  • 失败,使用
    继续
    继续重试
  • 代码块:

    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
    
  • 控制台输出:

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

这个用例 您可以遵循类似的逻辑,您的有效代码块将是:

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