[英]AWS EC2 times out when trying to authenticate using a selenium headless chrome
我正在嘗試使用 AWS 來自動執行某些程序,並且需要使用我正在使用的 API 對自己進行身份驗證。 問題是,它的速度太慢了。 我正在使用 selenium 無頭 chrome 瀏覽器連接到頁面,填寫我需要的信息並提交表格(根據其他來源我發現 TD Ameritrade API 只接受瀏覽器路由所以沒有辦法只是有請求,如果你知道的話,那也很好)。 在頁面上查找元素大約需要 20 秒,發送密鑰可能需要整整一分鍾,而嘗試提交表單會在 5 分鍾后超時。 這是我實施 selenium 的問題,還是與 AWS 或我正在連接的 API 有關? 我的代碼如下圖,報錯:
oauth = OAuth2Session(client_code, redirect_uri=redirect)
authorization_url, state = oauth.authorization_url('https://auth.tdameritrade.com/auth')
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1280x1696')
chrome_options.add_argument('--user-data-dir=tmp/user-data')
chrome_options.add_argument('--hide-scrollbars')
chrome_options.add_argument('--enable-logging')
chrome_options.add_argument('--log-level=0')
chrome_options.add_argument('--v=99')
chrome_options.add_argument('--single-process')
chrome_options.add_argument('--data-path=tmp/data-path')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--homedir=tmp')
chrome_options.add_argument('--disk-cache-dir=tmp/cache-dir')
chrome_options.add_argument('--no-proxy-server')
chrome_options.add_argument("--proxy-server='direct://'");
chrome_options.add_argument("--proxy-bypass-list=*");
chrome_options.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
chrome_options.binary_location = "/usr/bin/chromium-browser"
with webdriver.Chrome(options=chrome_options) as driver:
driver.get(authorization_url)
usernamebox = driver.find_element_by_id("username0")
print("found")
usernamebox.send_keys(username)
print("sent")
passbox = driver.find_element_by_id("password")
print("found2")
passbox.send_keys(password)
print("sent2")
passbox.submit()
print("submitted")
錯誤:
Traceback (most recent call last):
File "/home/ubuntu/environment/td_ameritrade_test.py", line 229, in <module>
td = TDClient()
File "/home/ubuntu/environment/td_ameritrade_test.py", line 172, in __init__
self.oauth_client = self.authenticate_user()
File "/home/ubuntu/environment/td_ameritrade_test.py", line 90, in authenticate_user
passbox.submit()
File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 86, in submit
self._parent.execute_script(
File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 634, in execute_script
return self.execute(command, {
File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.6/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 300.000
(Session info: headless chrome=86.0.4240.75)
任何幫助將不勝感激!
關於您的用例的更多詳細信息將有助於我們分析程序執行速度過慢背后的原因。 以下是一些注意事項:
root
用戶( administrator
)身份運行 Chrome。雖然可以通過在創建 WebDriver session 時傳遞 --no-sandbox 標志來解決此問題,但此類配置不受支持,並且非常氣餒。 您需要將環境配置為以普通用戶身份運行 Chrome。 因此,您可能需要刪除--no-sandbox
選項。這是Sandbox故事的鏈接。
--headless
選項時,由於某些限制,您將無法使用--window-size=1280x1696
。您可以在以下位置找到一些相關的詳細討論:
--disable-gpu
的參數是在windows平台上啟用google-chrome-headless 。 它是必需的,因為SwiftShader 之前在無頭模式下無法對 Windows 進行斷言。 此問題已通過 Headless解決:make --disable-gpu flag unnecessary--hide-scrollbars
、 --enable-logging
、 --log-level=0
、 --v=99
、 --single-process
、 --data-path=tmp/data-path
的任何特定要求--data-path=tmp/data-path
, --homedir=tmp
, --disk-cache-dir=tmp/cache-dir
, --no-proxy-server
, --proxy-server='direct://'
和--proxy-bypass-list=*
arguments 您選擇暫時刪除並根據您的測試規范將它們添加回來。您可以在以下位置找到一些相關的詳細討論:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.