日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案

發布時間:2025/4/16 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

driver.set_page_load_timeout(3) 頁面加載時間設置 3 秒,執行到某一步涉及頁面加載如果加載時間超過 3 秒就會停止加載并拋出異常,其實這個時候頁面內的元素已經加載出來了,我們在這一步進行異常捕獲不讓程序停止,然后直接執行下一步即可。

報錯信息如下:
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 3.000

def analyze_jira(driver, d):# 方案一:異常捕獲方案# 頁面加載時間設置,超時會直接報錯,將會報錯的地方加個異常不過,完美解決問題driver.set_page_load_timeout(3)for (k,v) in d.items():driver.find_element_by_xpath('//input[@id="quickSearchInput"]').send_keys(k[4:])try:# 查詢操作,耗時很長ActionChains(driver).send_keys(Keys.ENTER).perform()except Exception as e:print("抓到異常,頁面停止加載,但是程序不停止。")time.sleep(1)# 提取頁面指定元素的文本question_zhuti = driver.find_element_by_xpath('//*[@id="summary-val"]').text;

還可以通過 set_script_timeout() 方法來解決問題。

def analyze_jira(driver, d):# 方案二:同時設置腳本執行超時時間方案# 設置腳本報錯之前的等待時間,這個小于等于上面set_page_load_timeout()設置的時間就不會拋錯。driver.set_page_load_timeout(3)driver.set_script_timeout(3)for (k,v) in d.items():driver.find_element_by_xpath('//input[@id="quickSearchInput"]').send_keys(k[4:])# 查詢操作,耗時很長ActionChains(driver).send_keys(Keys.ENTER).perform()time.sleep(1)# 提取頁面指定元素的文本question_zhuti = driver.find_element_by_xpath('//*[@id="summary-val"]').text;

set_page_load_timeout()
方法說明:
Set the amount of time to wait for a page load to complete before throwing an error.
翻譯:
設置在拋出錯誤之前等待頁面加載完成的時間。

set_script_timeout()
方法說明:
Set the amount of time that the script should wait during an execute_async_script call before throwing an error.
翻譯:
設置腳本在execute_async_script調用期間拋出錯誤之前應該等待的時間。

喜歡的點個贊?吧!

總結

以上是生活随笔為你收集整理的Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。