简体   繁体   English

在 GCP Cloud Run 上使用 Selenium 运行 Python 脚本时出现问题

[英]Issue when running Python script with Selenium over GCP Cloud Run

I have this python script in which I need to login and retrieve an access_token from an authentication server, the process is as follows, navigate to the authentication server with a given URL, embed given username and password in corresponding fields, click 'login', access_token should be in the refreshed URL, so wait for 5 seconds and then retrieve the updated URL, slice it and get the access_token.我有这个 python 脚本,我需要在其中登录并从身份验证服务器检索 access_token,过程如下,使用给定的 URL 导航到身份验证服务器,在相应字段中嵌入给定的用户名和密码,单击“登录”, access_token应该在刷新后的URL中,所以等待5秒再取出更新后的URL,切片得到access_token。

On my local machine I was able to retrieve the Token and successfully complete the script.在我的本地机器上,我能够检索令牌并成功完成脚本。

However I want to deploy this script to Cloud Run, so I created the DockerFile and the python dependencies "requirment.txt" which I believe are accurate.但是,我想将此脚本部署到 Cloud Run,因此我创建了 DockerFile 和 python 依赖项“requirment.txt”,我认为这是准确的。

After the whole thing is deployed and, I try to run the service from Cloud Scheduler as usual, I get this error整个部署完成后,我尝试像往常一样从 Cloud Scheduler 运行服务,但出现此错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="email"]"}
运行服务后出错

Python code from my local machine, working perfectly我本地机器上的 Python 代码,完美运行在此处输入图像描述

DockerFile deployed to cloud run DockerFile 部署到云端运行DockerFile

Python Requirements and Dependencies Python 要求和依赖关系要求.txt

Update: The next day I found that the issue came back, and the run failed:/ same error更新:第二天发现问题又来了,运行失败:/同样的错误第二天失败

The success of the day before前一天的成功前一天的成功

I figured it out, apparently, the implicit wait I had wouldn't be caught on GCP, would work on my local however, which is weird, I needed to implement an explicit - one line such as this.我想通了,显然,我的隐式等待不会在 GCP 上被捕获,但会在我的本地工作,这很奇怪,我需要实现一个显式的 - 一行这样的。

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait


WebDriverWait(browser, 50).until(EC.presence_of_element_located((By.ID, "app")))

在此处输入图像描述

Basically what it did is wait till the page is refreshed and an element from the next page is returned to give the URL variable to the slicing part of my code.基本上它所做的就是等到刷新页面并返回下一页中的元素以将 URL 变量提供给我的代码的切片部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 GCP 设置文档运行“Hello Cloud Run with Python”时,日志中出现“[CRITICAL] WORKER TIMEOUT” - "[CRITICAL] WORKER TIMEOUT" in logs when running "Hello Cloud Run with Python" from GCP Setup Docs 在 GCP 上运行基本 Python 脚本 - Running basic Python script on GCP 在谷歌云上运行 python 脚本时出现内存错误 - MemoryError when running python script on google cloud 如何在云中运行 Selenium Python 脚本? - How can I run a Selenium Python Script in the Cloud? 运行python脚本Phantomjs和Selenium时出现超时问题 - Timeout issue while running python script Phantomjs and Selenium 如何在 GCP 的 Cron 中运行 Python 脚本? - How to run Python script in Cron in GCP? 如何在 GCP 中每天运行 Python 脚本? - How to run a Python script every day in GCP? GCP 云运行 - Python 代码在崩溃时不会留下堆栈跟踪 - GCP cloud-run - Python code does not leave stack-trace when it crashes 从命令行运行 python 脚本时找不到 Selenium - Selenium cannot be found when running the python script from CommandLine “Firefox意外退出。”在Python中运行基本的Selenium脚本时 - “Firefox quit unexpectedly.” when running basic Selenium script in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM