Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案
driver.set_page_load_timeout(3) 頁面加載時間設置 3 秒,執行到某一步涉及頁面加載如果加載時間超過 3 秒就會停止加載并拋出異常,其實這個時候頁面內的元素已經加載出來了,我們在這一步進行異常捕獲不讓程序停止,然后直接執行下一步即可。
報錯信息如下:
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 3.000
還可以通過 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自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 技术篇-通过管道命令获取c
- 下一篇: Mac系统修改root用户密码,mac切