我正在测试一个网站,除了页面花费太多时间,然后硒继续搜索元素之外,一切都很好。我尝试使用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)
您可以在如何为 python selenium 3.8.0 设置"driver.get"的超时中找到详细的讨论?
但是值得一提的是,set_page_load_timeout()
仅在"document.readyState">等于"complete"
后才适用。也许在这个用例中,'document.readyState'
等于"complete"
很晚才实现。
溶液
作为解决方案,而不是使用默认设置为normal
的默认pageLoadStrategy
,您可以将pageLoadStrategy
设置为eager
即交互式,如下所示:
caps["pageLoadStrategy"] = "eager" # interactive
您可以在如何使Selenium不等到整页加载中找到详细的讨论,该脚本很慢?