How to access a site which is already launched in chrome browser using selenium / python?

770 views
Skip to first unread message

Guruprasad Achar

unread,
Jul 11, 2022, 8:51:20 AM7/11/22
to Selenium Users
I am interested to run test on a site which is already opened in a browser manually. I want to avoid login to the site via script.

I have tried one approach to see if I can do similar by visiting gmail inbox via selenium using cookies :
  1. I login to gmail.
  2. Read cookies from console by running document.cookie .
  3. From selenium (using Python) after allocating browser, visit google host and add same cookies
  4. Revisit google mail inbox.
Following is the code used

import platform
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

driver_path = {
            'Linux' : {
                'chrome': 'assets/chromedriver'
            },
            'Windows': {
                'chrome': 'assets/chromedriver.exe'
            }
        }

cookies = ['GMAIL_AT=xxxxxx',
           'SEARCH_SAMESITE=xxxx',
           'APISID=yydfsfd',
           'SAPISID=zzzzzz',
           '__Secure-1PAPISID=zxzcczzz',
           '__Secure-3PAPISID=zzzzzzz',
           'SID=zzzzzzz',
           'OGPC=2342342:',
           'OGP=-523232:',
           '1P_JAR=2022-07-09-11',
           '__Host-GMAIL_SCH=nsl',
           'SIDCC=eeeeeeeeeee']

def allocate_browser(name = 'chrome'):
    chrome_options = webdriver.ChromeOptions()
    service = Service(driver_path[platform.system()][name])
    driverHandler = webdriver.Chrome(service=service,options=chrome_options)
    driverHandler.maximize_window()
    return driverHandler

def test_login_page():
    chrome_browser = allocate_browser()
    chrome_browser.get('https://mail.google.com')
    for cookie in cookies:
        entry = cookie.split('=')
        chrome_browser.add_cookie({'name':entry[0],'value':entry[1]})
    chrome_browser.get('https://mail.google.com/mail/u/0/#inbox/')
    time.sleep(10)
    chrome_browser.close()

Above approach was not fruitful. Any other suggestions ?

SiKing

unread,
Jul 11, 2022, 6:54:25 PM7/11/22
to Selenium Users
Reply all
Reply to author
Forward
0 new messages