How to save the session of Whatsapp web with Selenium VB.NET

  Kiến thức lập trình

I want to scan the QR code and log in to WhatsApp

I want when I click the Save Session button, the session will be saved

So that when I close the program and then reopen it and select the session and press the Load Session button, the session will be opened directly without me logging in using the QR Code every time.

Now you have created a named button

SaveSessionButton so that when I click on it, it saves the browser session as we did previously

And a button called LoadSessionButton to retrieve the saved session and open it

The combobox is named SavedSessionsComboBox so that the saved session can be identified before opening it

Now I want to update this code to do it correctly

Imports System.ComponentModel
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Threading
Imports Guna.UI.WinForms
Imports Guna.UI2.WinForms
Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports ZstdSharp.Unsafe
Public Class WhatsappFilter
    Private driverService As ChromeDriverService
    Dim Started As Boolean = True
    Dim StartedChatWaiting As Boolean = False
    Dim Started2 As String = "False"
    Dim threads As New List(Of Thread)()
    Dim r As New Random
    Dim backgroundWorker As BackgroundWorker
    Private Sub WhatsappFilter_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
    End Sub
    Sub StartSeleniumTask(ByRef drivers() As IWebDriver, ByVal index As Integer)

        driverService = ChromeDriverService.CreateDefaultService()
        driverService.HideCommandPromptWindow = True
        Dim options As ChromeOptions = New ChromeOptions()
        options.AddArguments("--incognito")
        Dim id As String = Thread.CurrentThread.ManagedThreadId
        drivers(index) = New ChromeDriver(driverService, options)
        drivers(index).Navigate.GoToUrl("https://web.whatsapp.com/")


    End Sub

    Private Sub Guna2Button6_Click(sender As Object, e As EventArgs) Handles Guna2Button6.Click
        Started2 = "True"
    End Sub
End Class

I tried to add this but it didn’t work and it didn’t save anything

Imports System.IO
Imports System.Threading
Imports OpenQA.Selenium
Imports OpenQA.Selenium.Chrome
Imports System.ComponentModel

Public Class WhatsappFilter
    Private SavedSessions As New Dictionary(Of String, String)

    Private Sub SaveSession(ByVal sessionName As String, ByVal folderPath As String)
        SavedSessions.Add(sessionName, folderPath)
        SavedSessionsComboBox.Items.Add(sessionName)
    End Sub

    Private Sub LoadSession(ByVal sessionName As String)
        If SavedSessions.ContainsKey(sessionName) Then
            Dim folderPath As String = SavedSessions(sessionName)
            Dim processId As Integer = OpenWhatsAppWeb("https://web.whatsapp.com/", 1, folderPath)
            ' You may want to return the processId or do something with it
        Else
            MessageBox.Show("Session not found.")
        End If
    End Sub

    Public Shared Function OpenWhatsAppWeb(ByVal website As String, ByVal timeToWaitInMinutes As Integer, ByVal folderPathToStoreSession As String) As Integer
        Dim options As ChromeOptions = Nothing
        Dim driver As ChromeDriver = Nothing
        Try
            Dim processId As Integer = -1
            Dim timeToWait As TimeSpan = TimeSpan.FromMinutes(timeToWaitInMinutes)
            Dim cService As ChromeDriverService = ChromeDriverService.CreateDefaultService()
            cService.HideCommandPromptWindow = True

            options = New ChromeOptions()
            options.AddArgument("--user-data-dir=" & folderPathToStoreSession)
            options.AddArgument("--disable-extensions")

            driver = New ChromeDriver(cService, options, timeToWait)
            processId = cService.ProcessId

            driver.Navigate.GoToUrl(website)

            ' You may add additional wait here if needed
            Thread.Sleep(5000)

            Dim result As DialogResult = MessageBox.Show("Do you want to exit?", "", MessageBoxButtons.YesNo)
            If result = DialogResult.Yes Then
                If driver IsNot Nothing Then
                    driver.Close()
                    driver.Quit()
                    driver.Dispose()
                End If
                Return processId
            End If

            Return -1
        Catch ex As Exception
            Console.WriteLine("Exception occurred: " & ex.Message)
            If driver IsNot Nothing Then
                driver.Close()
                driver.Quit()
                driver.Dispose()
            End If
            driver = Nothing
            Throw
        End Try
    End Function

    Private Sub SaveSessionButton_Click(sender As Object, e As EventArgs) Handles SaveSessionButton.Click
        Dim sessionName As String = InputBox("Enter session name:", "Save Session")
        If Not String.IsNullOrEmpty(sessionName) Then
            Dim folderPathToStoreSession As String = Path.Combine(Application.StartupPath, sessionName)
            SaveSession(sessionName, folderPathToStoreSession)
        End If
    End Sub

    Private Sub LoadSessionButton_Click(sender As Object, e As EventArgs) Handles LoadSessionButton.Click
        Dim selectedSession As String = SavedSessionsComboBox.SelectedItem.ToString()
        LoadSession(selectedSession)
    End Sub
End Class

New contributor

Mohamed Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT