首页 > 解决方案 > 在 python 中使用 Selenium 完全加载运行命令而不等待页面

问题描述

我有 Selenium 的自动化,我想在打开链接后运行一些命令,但不等待加载页面,我想运行这些代码,即使页面没有完全加载,或者在页面加载时

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path=r"C:\Users\Gabri\anaconda3\chromedriver.exe")
wait: WebDriverWait = WebDriverWait(driver, 10)
driver.get('https://stackoverflow.com/')

# I want to run some commands in this lines, without wait the page load
...


# I did this wait just for print when the page completely load
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,
                                           'body > header > div > div.-main.flex--item > a.-logo.js-gps-track > span')))

print('The page is completely load')
driver.quit()

我要运行的命令...与页面无关,这就是为什么我需要在不等待页面加载的情况下运行

标签: pythonseleniumselenium-webdriver

解决方案


在不等待页面加载的情况下运行这些方法的最简单方法是在涉及driver.get('https://stackoverflow.com/')方法之前运行它们


推荐阅读