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 :
- I login to gmail.
- Read cookies from console by running document.cookie .
- From selenium (using Python) after allocating browser, visit google host and add same cookies
- 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 ?