【发布时间】:2021-05-24 12:57:01
【问题描述】:
我正在尝试使用 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)
任何帮助将不胜感激!
【问题讨论】:
-
你真的需要那么多
add_argument吗? -
这些是我在 AWS 中使用 selenium 的唯一指南中找到的选项,尽管这些指南都描述了网络爬虫,但您的权利其中一些论点可能毫无用处。您认为这会减慢速度吗?
-
粗略设置太多参数会对测试执行产生负面影响。根据 Test Specs 使用参数。
-
好吧,我不知道 Stack Overflow 是如何工作的,但是这很神奇,我只使用了 headless 参数,而且效果很好。非常感谢。
标签: python selenium google-chrome amazon-ec2 selenium-chromedriver