我正在测试一个网站,一切都很好,除非页面花费了太多时间,然后 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()