Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案
driver.set_page_load_timeout(3) 頁(yè)面加載時(shí)間設(shè)置 3 秒,執(zhí)行到某一步涉及頁(yè)面加載如果加載時(shí)間超過(guò) 3 秒就會(huì)停止加載并拋出異常,其實(shí)這個(gè)時(shí)候頁(yè)面內(nèi)的元素已經(jīng)加載出來(lái)了,我們?cè)谶@一步進(jìn)行異常捕獲不讓程序停止,然后直接執(zhí)行下一步即可。
報(bào)錯(cuò)信息如下:
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 3.000
還可以通過(guò) set_script_timeout() 方法來(lái)解決問(wèn)題。
def analyze_jira(driver, d):# 方案二:同時(shí)設(shè)置腳本執(zhí)行超時(shí)時(shí)間方案# 設(shè)置腳本報(bào)錯(cuò)之前的等待時(shí)間,這個(gè)小于等于上面set_page_load_timeout()設(shè)置的時(shí)間就不會(huì)拋錯(cuò)。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:])# 查詢操作,耗時(shí)很長(zhǎng)ActionChains(driver).send_keys(Keys.ENTER).perform()time.sleep(1)# 提取頁(yè)面指定元素的文本question_zhuti = driver.find_element_by_xpath('//*[@id="summary-val"]').text; set_page_load_timeout()
方法說(shuō)明:
Set the amount of time to wait for a page load to complete before throwing an error.
翻譯:
設(shè)置在拋出錯(cuò)誤之前等待頁(yè)面加載完成的時(shí)間。
set_script_timeout()
方法說(shuō)明:
Set the amount of time that the script should wait during an execute_async_script call before throwing an error.
翻譯:
設(shè)置腳本在execute_async_script調(diào)用期間拋出錯(cuò)誤之前應(yīng)該等待的時(shí)間。
喜歡的點(diǎn)個(gè)贊?吧!
總結(jié)
以上是生活随笔為你收集整理的Python+selenium自动化:页面加载慢、超时加载情况下内容已经加载完毕的快速执行脚本解决方案,页面加载时间过长优化方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python 技术篇-通过管道命令获取c
- 下一篇: Python操作excel进行插入、删除