Python 等待无头chrome中包含元素的网页时超时异常
以下代码使用非无头铬合金,可正常工作:Python 等待无头chrome中包含元素的网页时超时异常,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,以下代码使用非无头铬合金,可正常工作: import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support import expe
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--start-maximized')
chrome_options.binary_location = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(executable_path=os.path.abspath("C:\User\Program Files\chrome-driver\chromedriver.exe"))
driver.set_window_size(1200, 600)
driver.get("login-url")
driver.find_element_by_id("loginId").send_keys("uname")
driver.find_element_by_id("newPassword").send_keys("pwd")
driver.find_element_by_name("submit-button").click()
driver.set_window_size(1200, 800)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
v = driver.find_element_by_xpath("//tr[4]/td[5]/span").text
print(v)
当我选择使用无头镀铬时:
driver = webdriver.Chrome(executable_path=os.path.abspath("C:\User\Program Files\chrome-driver\chromedriver.exe"), chrome_options=chrome_options)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
它引发以下异常:
Traceback (most recent call last):
File "C:/User/workspaces/pyworkspaces/fin2/venv/process.py", line 28, in <module>
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
File "C:\User\workspaces\pyworkspaces\fin2\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
我还尝试了显示\u元素\u的位置
:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"user-info")))
但它仍然给出了
TimeOutException
。为什么会这样?我过去所做的是添加参数“--start maximized”:
chrome_options.add_argument('--start-maximized')
试试看,希望对你有帮助 我过去所做的是添加参数“--start maximized”:
chrome_options.add_argument('--start-maximized')
试试看,希望对你有帮助 您需要等待
元素位置()的可见性,而不是使用元素位置()的存在性
,您可以使用以下方法:
- 使用
ID
:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
- 使用
CSS\u选择器
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#user-info")))
- 使用
XPATH
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='user-info']")))
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您需要等待元素位置的可见性,而不是使用元素位置的存在()
,您可以使用以下方法:
- 使用
ID
:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
- 使用
CSS\u选择器
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#user-info")))
- 使用
XPATH
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='user-info']")))
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我没有得到“因为您打算在元素中发送字符序列”。我只想等到一个包含id=user info
元素的网页加载。尝试将WebDriverWait(driver,10).替换为WebDriverWait(driver,20).直到(EC.element可点击((By.XPATH,“/*[@ID='user-info']))
)
。给出相同的例外情况。@如果您没有提到您在等待获得包含该元素的网页之后打算做什么的特定用例。。。。最初我假设它是UserID字段,但现在它似乎是userinfo字段。所以改变了密码。让我知道情况。我错问了同样的问题。现在复制了这个问题的内容,因为它是用堆栈跟踪写的。你上面的答案解决了吗?我没有得到“因为你想在元素中发送一个字符序列”。我只想等到一个包含id=user info
元素的网页加载。尝试将WebDriverWait(driver,10).替换为WebDriverWait(driver,20).直到(EC.element可点击((By.XPATH,“/*[@ID='user-info']))
)
。给出相同的例外情况。@如果您没有提到您在等待获得包含该元素的网页之后打算做什么的特定用例。。。。最初我假设它是UserID字段,但现在它似乎是userinfo字段。所以改变了密码。让我知道情况。我错问了同样的问题。现在复制了这个问题的内容,因为它是用堆栈跟踪写的。你的上述答案能解决这个问题吗?