使用 headless chrome webdriver 时出现超时异常错误
timeout exception error on using headless chrome webdriver
我的 selenium 代码在没有无头模式的情况下工作,但是当我尝试使用无头模式时,它引发了 TimeOutException 错误。我正在使用 python selenium chrome webdriver。我正在地址为 http://127.0.0.1:5000/ 的本地机器上进行测试
我正在尝试使用 selenium 登录 Instagram,但它无法在无头模式下工作。我尝试调整 window 的大小(最大化,手动设置大小,resize_to)但其中 none 有效。
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("—disable-gpu")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_driver = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
driver.set_window_size(1382, 744)
我正在使用下面的辅助函数来等待元素
def waiting(xpath):
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
return element
我登录 Instagram 的代码如下:
driver.get("https://www.instagram.com")
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
username.send_keys("abcdeff")
password = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input")
password.send_keys("password")
driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
我收到这个错误:
Traceback (most recent call last):
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 47, in login
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 30, in waiting
element = WebDriverWait(driver, 20).until(
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
您在第 username
步遇到错误。我通常不喜欢使用“WebDriverWait()....until(.....)”。我总是使用:
import time
...
...
time.sleep(10)
...
...
这总能解决我的问题,而且效率很高!
正如 Swaroop 提到的,您应该尽可能避免使用完整的 xpath
!代码可能会崩溃。
我试过下面的代码,它对我有用。请试一试,如果它解决了您的问题,请将其标记为答案。请记住在代码中更改 yourUserName 和 YourPassword。
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 30)
action = ActionChains(driver)
driver.get("https://www.instagram.com/")
wait.until(EC.presence_of_element_located((By.NAME, "username"))).send_keys("yourUserName")
wait.until(EC.presence_of_element_located((By.NAME, "password"))).send_keys("YourPassword")
wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Log In']/parent::button"))).click()
print(driver.current_url)
注意 - Instagram 检测到您在无头模式下使用 selenium。为避免这种情况,您可以使用像 options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
.
这样的假用户代理
希望它能解决您的问题。谢谢!
我的 selenium 代码在没有无头模式的情况下工作,但是当我尝试使用无头模式时,它引发了 TimeOutException 错误。我正在使用 python selenium chrome webdriver。我正在地址为 http://127.0.0.1:5000/ 的本地机器上进行测试 我正在尝试使用 selenium 登录 Instagram,但它无法在无头模式下工作。我尝试调整 window 的大小(最大化,手动设置大小,resize_to)但其中 none 有效。
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("—disable-gpu")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_driver = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
driver.set_window_size(1382, 744)
我正在使用下面的辅助函数来等待元素
def waiting(xpath):
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
return element
我登录 Instagram 的代码如下:
driver.get("https://www.instagram.com")
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
username.send_keys("abcdeff")
password = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input")
password.send_keys("password")
driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
我收到这个错误:
Traceback (most recent call last):
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 47, in login
username = waiting("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
File "C:\Users\madhav\Desktop\Fprojects\insta-sam\temp.py", line 30, in waiting
element = WebDriverWait(driver, 20).until(
File "C:\Users\madhav\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
您在第 username
步遇到错误。我通常不喜欢使用“WebDriverWait()....until(.....)”。我总是使用:
import time
...
...
time.sleep(10)
...
...
这总能解决我的问题,而且效率很高!
正如 Swaroop 提到的,您应该尽可能避免使用完整的 xpath
!代码可能会崩溃。
我试过下面的代码,它对我有用。请试一试,如果它解决了您的问题,请将其标记为答案。请记住在代码中更改 yourUserName 和 YourPassword。
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 30)
action = ActionChains(driver)
driver.get("https://www.instagram.com/")
wait.until(EC.presence_of_element_located((By.NAME, "username"))).send_keys("yourUserName")
wait.until(EC.presence_of_element_located((By.NAME, "password"))).send_keys("YourPassword")
wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Log In']/parent::button"))).click()
print(driver.current_url)
注意 - Instagram 检测到您在无头模式下使用 selenium。为避免这种情况,您可以使用像 options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
.
希望它能解决您的问题。谢谢!