![](/img/trans.png)
[英]Timeout Error occurred When run a script on Headless chrome browser by using Selenium Webdriver with Python
[英]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),但它們都不起作用。
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)
我在下面使用這個助手 function 來等待元素
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:
我已經嘗試了下面的代碼,它對我有用。 請試一試,如果它解決了您的問題,請將其標記為答案。 請記住更改代碼中的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")
.
希望它能解決您的問題。 謝謝。!
您在步驟username
收到錯誤。 我一般不喜歡使用“WebDriverWait()....until(.....)”。 我總是使用:
import time
...
...
time.sleep(10)
...
...
這總能解決我的問題,而且效率也很高,正如 Swaroop 所提到的,您應該盡可能避免使用完整的xpath
。 代碼可能會崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.