gpt4 book ai didi

python - 如果在selenium python中加载页面花费太长时间,如何停止测试

转载 作者:太空宇宙 更新时间:2023-11-03 19:58:07 27 4
gpt4 key购买 nike

我正在测试一个网站,一切都很好,除了当页面花费太多时间时,selenium 会继续搜索元素。我尝试使用 driver.set_page_load_timeout(5) 但仍然继续加载。
这是代码:

from selenium import webdriver
from selenium.common.exceptions import *
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time, os,re, datetime
import logging




class SanityTest():

def login_test(self):
driver_location = 'C:\\chromedriver.exe'
os.environ["webdriver.chrome.driver"] = driver_location
driver = webdriver.Chrome(driver_location)
driver.maximize_window()
driver.implicitly_wait(0)
baseURL = driver.get("https://www.example.com/")
logging.basicConfig( format = '%(asctime)s: %(levelname)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p'
,level=logging.INFO)
driver.set_page_load_timeout(2)

#Is Platform is reachable?:-
try:
if WebDriverWait(driver, 1).until(
EC.visibility_of_element_located((By.XPATH, "//button[contains(@class,'button')]"))):
logging.info("Success")
except:
pass

try:
if WebDriverWait(driver, 1).until(
EC.visibility_of_element_located((By.XPATH, '//span[@jsselect="heading" and @jsvalues=".innerHTML:msg"]'))):

logging.error('Failure: Unable to reach platform! ==> ' + str(driver.find_element_by_xpath('//span[@jsselect="heading" and @jsvalues=".innerHTML:msg"]').text))
return
except :
print("Page is taking too long to load")
return



ff = SanityTest()
ff.login_test()

最佳答案

功能上set_page_load_timeout()有效并且应该对你有效:

driver.set_page_load_timeout(2)

You can find a detailed discussion in How to set the timeout of 'driver.get' for python selenium 3.8.0?

但是值得一提的是set_page_load_timeout()仅在 'document.readyState' 等于 "complete" 后适用 已实现。也许在这个用例中'document.readyState'等于"complete"实现得很晚。

<小时/>

解决方案

作为解决方案,而不是使用默认的 pageLoadStrategy默认设置为 normal ,可以设置pageLoadStrategy作为 eager 交互式如下:

caps["pageLoadStrategy"] = "eager"  #  interactive

You can find a detailed discussion in How to make Selenium not wait till full page load, which has a slow script?

关于python - 如果在selenium python中加载页面花费太长时间,如何停止测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com