Home > Mobile >  C# selenium chrome driver, every time I run my program I need to scan QR code from my mobile
C# selenium chrome driver, every time I run my program I need to scan QR code from my mobile

Time:01-28

ChromeOptions options = new ChromeOptions();
options.AddArguments("--user-data-dir=C:\\Users\\Myname\\AppData\\Local\\Google\\Chrome\\User Data");
options.AddArguments("--profile-directory=Profile 3"); 
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");

How can I store the whatsApp login session, So that every time I run a program I don't need to scan the QR code again?

CodePudding user response:

When you quit the webdriver, the cookies, cache, history, etc are deleted. So if you want to be already logged in when you start the webdriver, you have to use the cookies from a profile on the normal browser in which you are already logged in. To do so you have to:

  • create a new chrome profile in the normal browser
  • login to whatsapp
  • load the profile in selenium

In this way when you start the webdriver and load the page you will be already logged in.

  1. To create a new profile, open your chrome browser, click on the profile icon on top right, then click "Add" and then click "Continue without an account".

enter image description here

  1. Then write a name for your profile and be sure check the box "Create a desktop shortcut". I suggest you to choose a color so that you will immediately see if it works when selenium open the browser window.

enter image description here

  1. Open chrome from the new desktop icon (mine is called "PythonSelenium - Chrome.exe") and login to instagram.

    After you logged in, close the browser and open the properties of the new desktop shortcut. Take note of the name of the profile directory, which in my case is "Profile 3".

enter image description here

  1. Now we have to tell selenium to open the chrome driver using this new profile we've just created, which is logged in twitch. To do this we need the path of the folder where the profile is stored. By default the path is something like C:\Users\your_username\AppData\Local\Google\Chrome\User Data, if you are not sure about it check where chrome is installed in your computer.

Then run this code (remember to substitute Profile 3 with the name of your profile from step 3)

ChromeOptions options = new ChromeOptions();
options.AddArguments("--no-sandbox") 
options.AddArguments("--remote-debugging-port=9222")
options.AddArguments("--disable-dev-shm-using")
options.AddArguments("--disable-setuid-sandbox")
options.AddArguments("--user-data-dir=C:\Users\your_username\AppData\Local\Google\Chrome\User Data");
options.AddArguments("--profile-directory=Profile 3"); // <-- substitute Profile 3 with your profile name
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
  • Related