Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using selenium: How to keep logged in after closing Driver in Python

I want to get my Whatsapp web (web.whatsapp.com) logged in, at the second time opening the Whatsapp web on chrome driver. Following is my code based on Python need your help.

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


chrome_path = r"chromedriver.exe"
options = Options();
options.add_argument("user-data-
dir=C:/Users/Username/AppData/Local/Google/Chrome/User Data");
#options.add_argument("--start-maximized");
driver = webdriver.Chrome(chrome_path,chrome_options=options);

#driver = webdriver.Chrome();
driver.get('https://web.whatsapp.com/')
like image 980
Daniyal Tariq Avatar asked Aug 12 '17 15:08

Daniyal Tariq


People also ask

How do I save a login session in Selenium?

You have to save the response cookies in pickle or h5 file using the dump method and read the file when you need the cookies again. The method is just simple as writing a text file.

Can you run Selenium in the background?

Selenium Webdriver can run in the background, i.e. without opening a browser window, by running it in Headless mode. To do this you need to add the required capability to the set-up code for the driver.

Can Selenium interact with an existing browser session Python?

New Selenium IDEWe can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

How do you close active current tab without closing the browser in Selenium Python?

We can close the active/current tab without closing the browser in Selenium webdriver in Python. By default, Selenium has control over the parent window. Once another browser window is opened, we have to explicitly shift the control with the help of switch_to. window method.


1 Answers

I tried on my Mac, below code and it worked perfectly fine, I don't need to login again

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")
like image 60
Tarun Lalwani Avatar answered Sep 22 '22 08:09

Tarun Lalwani