在自动测试AJAX Web UI时,使用延迟可能会导致不稳定的测试结果和性能问题。因此,建议使用以下替代方法:
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10) # 设置隐式等待时间为10秒
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "my_element")))
from selenium import webdriver
driver = webdriver.Chrome()
while True:
num_requests = driver.execute_script("return window.performance.getEntries().length;")
requests = driver.execute_script("return window.performance.getEntries();")
active_requests = [r for r in requests if r["responseEnd"] == 0]
if num_requests == 0 or len(active_requests) == 0:
break
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.CHROME
caps["goog:loggingPrefs"] = {"performance": "ALL"}
driver = webdriver.Chrome(desired_capabilities=caps)
# 监听Network.requestWillBeSent事件
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd("Network.setRequestInterception", {"patterns": [{"urlPattern": "*"}]})
driver.execute_script("console.log('开始发送请求');")
driver.get("https://www.example.com/")
# 获取所有请求
logs = driver.get_log("performance")
for log_entry in logs:
if "Network.requestWillBeSent" in log_entry["message"]:
print(log_entry["message"])
# 监听Network.responseReceived事件
driver.execute_cdp_cmd("Network.enable", {})
driver.execute_cdp_cmd("Network.setRequestInterception", {"patterns": [{"urlPattern": "*"}]})
driver.execute_script("console.log('开始发送请求');")
driver.get("https://www.example.com/")
# 获取所有响应
logs = driver.get_log("performance")
for log_entry in logs:
if "Network.responseReceived" in log_entry["message"]:
print(log_entry["message"])
总之,在自动测试AJAX Web UI时,建议使用上述方法来替代延迟,以提高测试的稳定性和性能。
云原生正发声
云+社区技术沙龙[第14期]
Elastic 中国开发者大会
云+社区技术沙龙[第17期]
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第8期]
serverless days
T-Day
云+社区技术沙龙 [第31期]
领取专属 10元无门槛券
手把手带您无忧上云