等待网页中无头Chrome中的元素时发生超时异常 [英] Timeout exception while waiting for webpage with element in headless chrome
本文介绍了等待网页中无头Chrome中的元素时发生超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码使用无头镀铬,并且可以正常工作:
Following code uses non-headless chrome and it works:
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)
当我选择使用无头镀铬时:
When I choose to use headless chrome:
driver = webdriver.Chrome(executable_path=os.path.abspath("C:\User\Program Files\chrome-driver\chromedriver.exe"), chrome_options=chrome_options)
它引发以下异常:
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:
因此,它似乎在以下行中使用了无头的chrome失败了:
So it seems to fail on following line, in headless chrome:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
我也尝试过presence_of_element_located
:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"user-info")))
但是它仍然给出TimeOutException
.为什么会这样?
But it still gives TimeOutException
. Why is it so?
推荐答案
代替使用presence_of_element_located()
,您需要等待 visibility_of_element_located()
,并且可以使用以下
Instead of using presence_of_element_located()
you need to wait for visibility_of_element_located()
and you can use the following Locator Strategy:
-
使用
ID
:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID,"user-info")))
使用CSS_SELECTOR
:
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']")))
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
这篇关于等待网页中无头Chrome中的元素时发生超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文