简体   繁体   中英

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.

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.

After the whole thing is deployed and, I try to run the service from Cloud Scheduler as usual, I get this error

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在此处输入图像描述

DockerFile deployed to cloud run DockerFile

Python Requirements and Dependencies 要求.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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM