Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
Python 带selenium的递归函数_Python_Selenium_Selenium Webdriver_Exception_Web Scraping - Fatal编程技术网

Python 带selenium的递归函数

Python 带selenium的递归函数,python,selenium,selenium-webdriver,exception,web-scraping,Python,Selenium,Selenium Webdriver,Exception,Web Scraping,我试图在python中使用selenium实现一些web自动化,如果我的脚本遇到任何错误,我希望整个过程重新启动(无限循环) 因此,基本上我尝试使用递归函数,并在每次发生错误时调用它,结果如下所示: PATH = "C:\Program Files (x86)\chromedriver.exe" driver = webdriver.Chrome(PATH) def my_func(): try : driver.get("https://

我试图在python中使用selenium实现一些web自动化,如果我的脚本遇到任何错误,我希望整个过程重新启动(无限循环) 因此,基本上我尝试使用递归函数,并在每次发生错误时调用它,结果如下所示:

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

def my_func():
    try :
        driver.get("https://mywebsite.com")
        .
        .
        .
    except Exception as E : 
        print(str(E)) #printing the exception message
        driver.quit() #quitting from the current tab
        my_func() #recalling the function again 

my_func()

    
第一次一切正常时,当出现错误时(因为网站切换到另一个页面),它会打印此异常:
消息:element not interactiable
,这是完全正常的,但在第二次迭代中,我得到以下信息:

HTTPConnectionPool(host='127.0.0.1', port=59887): Max retries exceeded with url: /session/6d23ab3406dbef8f6581c4c7652d2633/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000019D2BA3CC10>: Failed to establish a new connection: `[WinError 10061] No connection could be made because the target machine actively refused it'))
HTTPConnectionPool(host='127.0.0.1',port=59887):url超过最大重试次数:/session/6d23ab3406dbef8f6581c4c7652d2633/url(由NewConnectionError引起(':未能建立新连接:`[WinError 10061]无法建立连接,因为目标计算机主动拒绝它'))
那么,有什么方法可以修复此错误或针对此脚本的其他更好的解决方案吗?

我认为,消息是:

No connection could be made because the target machine actively refused it

这可能是由目标URL导致的安全问题。尝试通过另一个连接(如移动热点)连接到internet。

我发现我必须在每次迭代中创建一个新的驱动程序,并且我在调用函数之前添加了一个time.sleep,以防请求发送过快:

def my_func():
    try :
        PATH = "C:\Program Files (x86)\chromedriver.exe"
        driver = webdriver.Chrome(PATH)
        driver.get("https://mywebsite.com")
        .
        .
        .
    except Exception as E : 
        print(str(E)) #printing the exception message
        driver.quit() #quitting from the current tab
        time.sleep(5)
        my_func() #recalling the function again 

my_func()

这只意味着你发送请求太快,需要增加一个时间。睡眠。