Issue when running Python script with Selenium over GCP Cloud Run

Viewed 57

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"]"}
Error after running service

Python code from my local machine, working perfectly enter image description here

DockerFile deployed to cloud run DockerFile

Python Requirements and Dependencies requirements.txt

1 Answers

Update: The next day I found that the issue came back, and the run failed :/ same error failed the next day

The success of the day before success of 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")))

enter image description here

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.

Related