简体   繁体   中英

selenium automation signing to google cloud throws networks error

I'm using selenium & selenium wire in my project. I'm writing flows to log in google cloud portals..

I enter my google cloud mail then, press on continue in Google sign in and then it log in to gcp.

I got some errors:

Request has invalid authentication credentials. Expected OAuth 2 access token, login cookie or other...

net:: ERR_PROXY_CONNECTION_FAILED

在此处输入图像描述

在此处输入图像描述

when I do the same flow manually without automation, with the same credentials, it works fine and no any.network error.

my web driver

from seleniumwire import webdriver
from seleniumwire.webdriver import ChromeOptions

def test_gcp_flow():

    options = ChromeOptions()
    options.add_experimental_option("detach", True)
    options.add_argument('--no-sandbox')
    options.add_argument('--single-process')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--start-maximized")
    options.add_argument('--auto-open-devtools-for-tabs')
    options.add_argument('--log-level=2')
    options.add_argument('--disable-features=IsolateOrigins,site-per-process')
    options.add_argument("--ignore_ssl")
    options.add_argument('--ignore-ssl-errors')
    options.add_argument('--ignore-certificate-errors')
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-setuid-sandbox")
    options.add_argument("--dns-prefetch-disable")
    options.add_argument('ignore-certificate-errors')
    options.add_argument('disable-web-security')
    options.add_argument('--allow-insecure-localhost')

    driver = webdriver.Chrome(options=options)
    driver.get('....any-hidden-url')
    # more flow actions - then it open gcp portal
    

I added openssl.cnf (without this openssl, it shows me TLS ssl issue) to run it locally in my test using Pycharm:

openssl_conf = openssl_init

[openssl_init]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
Options = UnsafeLegacyRenegotiation

I tried to add some change the chrome options that added to selenium driver but nothing has changed, still same error.

I tried to use Firefox webdriver and it works good without any.network issue. Maybe it's any chrome cache issue? because some days ago it worked me with chrome..

what I'm expecting is that to sign in to gmail without.network/token issues.

I can think of two possible options:

  1. Google detects Selenium and prevents login with it for security reasons. Well selenium web driver injects recognizable javascript to the browser, and you can detect it using a simple script.

  2. You have an authentication cookie in your browser that is not used with Selenium. Each Selenium session initiates itself completely clean. You can save your cookies with pickle for instance and then inject it to Selenium web driver with Python. Although I would avoid it if you cant store it encrypted in a secure place and fetch it during run time (or else - at some point it will be stolen no matter how much you think you are secured).

By the way if its something that you really need - YOU are a paying customer, maybe google has an available solutions for this kind of need.

Good luck!

Try using selenium wire with an undetected chrome-driver.

pip install selenium-wire

pip install undetected-chromedriver

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

if __name__ == "__main__":
    options = {}
    chrome_options = ChromeOptions()
    chrome_options.add_argument('--user-data-dir=hash')
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--incognito")
    chrome_options.add_argument("--disable-dev-shm-usage")
    browser = Chrome(seleniumwire_options=options, options=chrome_options)
    browser.get('your url')


    ... your rest of the code

I hope it resolves the issue.

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