1

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

1 回答 1

0

功能set_page_load_timeout()上有效,应该为您工作:

driver.set_page_load_timeout(2)

您可以在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,您可以设置pageLoadStrategyeager交互式,如下所示:

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

您可以在How to make Selenium not wait until full page load, which has a slow script?中找到详细讨论?

于 2019-12-21T21:07:05.907 回答