How can I maintain session after login using Selenium in Python?

Submitted 3 years, 7 months ago
Ticket #164
Views 1138
Language/Framework Python
Priority Medium
Status Closed

I'm succeeded logging in sites using Selenium in Python 3.4 The login process is like below:

browser = webdriver.PhantomJS()
browser.get('url')

emailElem = self.browser.find_element_by_name('user_id')
emailElem.send_keys(user_id)

passwordElem = self.browser.find_element_by_name('password') 
passwordElem.send_keys(user_pwd)

passwordElem.submit()

After I login, I'm trying to access certain page where the log-in session is required,

browser.get('login-session-required-page-url')

It lost its session and not allowed to access.

I think that this is related with session and cookies, but I have no idea how to deal with it.

Submitted on Sep 13, 20
add a comment

1 Answer

Verified

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


options = Options()
options.add_argument("user-data-dir=/tmp/tarun")
driver = webdriver.Chrome(chrome_options=options)

driver.get('https://web.whatsapp.com/')
driver.quit()

For window you can try changing the path as below

options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")

Submitted 3 years, 6 months ago


Latest Blogs