Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Automate WhatsApp Messages With Python using Pywhatkit module
Next article icon

Prerequisite:Selenium,Browser Automation Using Selenium

In this article, we are going to see how to share your Web-WhatsApp with anyone over the Internet without Scanning a QR code.

Web-Whatsapp store sessions

Web Whatsapp stores sessions in IndexedDB with the namewawc and syncs those key-value pairs to local storage. IndexedDB stores the data inside the user's browser and allows to create web application that can query from this indexedDB with or without a network connection. 

How to get a session:

We can figure it out by trying the following. We will take help ofchrome dev tool.

Steps:

  1. Clear cookies and refresh the page: It will remain login.
  2. Clear localStorage and refresh the page: It will restore the local Storage and remain logged in.
  3. Clear IndexedDB and refresh the page: It will restore the IndexedDB and remain logged in.
  4. Now clear both localStorage and IndexedDB: It will log out.

Steps demonstration:

Extract sessions from IndexedDB:

We can extract sessions from IndexedDB by using the following javascript. 

JavaScript
functiongetResultFromRequest(request){returnnewPromise((resolve,reject)=>{request.onsuccess=function(event){resolve(request.result);};});}asyncfunctiongetDB(){varrequest=window.indexedDB.open("wawc");returnawaitgetResultFromRequest(request);}asyncfunctionreadAllKeyValuePairs(){vardb=awaitgetDB();varobjectStore=db.transaction("user").objectStore("user");varrequest=objectStore.getAll();returnawaitgetResultFromRequest(request);}session=awaitreadAllKeyValuePairs();console.log(session);

We can try to execute the above code in the browser's console or tab where we had opened Web-Whatsapp and we will see the output as follows containing session key-value pairs.
 

Now we get those key-value pairs as text by running the following line of code.

JavaScript
JSON.stringify(session);

Now let's copy that text to a file to save a session and clear both localStorage and IndexedDB to log out. Now we can run the following code to inject a session by assigning the value of the session string we just copied to a file to variable SESSION_STRING. Then refresh the page and we will log in again without scanning the QR code. 

JavaScript
functiongetResultFromRequest(request){returnnewPromise((resolve,reject)=>{request.onsuccess=function(event){resolve(request.result);};})}asyncfunctiongetDB(){varrequest=window.indexedDB.open("wawc");returnawaitgetResultFromRequest(request);}asyncfunctioninjectSession(SESSION_STRING){varsession=JSON.parse(SESSION_STRING);vardb=awaitgetDB();varobjectStore=db.transaction("user","readwrite").objectStore("user");for(varkeyValueofsession){varrequest=objectStore.put(keyValue);awaitgetResultFromRequest(request);}}varSESSION_STRING="";awaitinjectSession(SESSION_STRING);


 

Automating the process of generating a session file and injecting a session:

We can automate the process of generating a session file that contains our session key-value pairs and also reading session key-value pairs from that generated session file to inject a session into the browser to open web-WhatsApp without scanning QR code

We can automate as follows:

Take the session file path as a command-line argument.

Generating session file:

  • Open Chrome browser. 
  • Open Web Whatsapp.
  • Ask the user to scan the QR code.
  • Wait for the QR code to be scanned.
  • Execute javascript in the browser and extract the session.
  • Save the file with a session text file with the custom file extension ".wa".
  • Close the browser.

Opening session file:

  • Verify that the session file exists.
  • Read the given file into the "session" variable. 
  • Open Chrome browser.
  • Open Web Whatsapp.
  • Wait for Web Whatsapp to be loaded properly.
  • Execute javascript in browser to inject session by using variable "session".
  • Refresh the page.
  • Ask for the user to press enter key to close the browser.

Below is the implementation:

Note:Please downloadchormedrive before running the code.

By using selenium's WebDriverWait, we can wait for some elements to be present on the browser as following:

Python3
def_wait_for_presence_of_an_element(browser,selector):element=Nonetry:element=WebDriverWait(browser,DEFAULT_WAIT).until(EC.presence_of_element_located(selector))except:passfinally:returnelement

Generating session file:

Python3
defsessionGenerator(sessionFilePath):# 1.1 Open Chrome browserbrowser=webdriver.Chrome()# 1.2 Open Web Whatsappbrowser.get("https://web.whatsapp.com/")# 1.3 Ask user to scan QR codeprint("Waiting for QR code scan...")# 1.4 Wait for QR code to be scanned_wait_for_presence_of_an_element(browser,MAIN_SEARCH_BAR__SEARCH_ICON)# 1.5 Execute javascript in browser and# extract the session textsession=browser.execute_script(EXTRACT_SESSION)# 1.6 Save file with session text file with# custom file extension ".wa"withopen(sessionFilePath,"w",encoding="utf-8")assessionFile:sessionFile.write(str(session))print("Your session file is saved to: "+sessionFilePath)# 1.7 Close the browserbrowser.close()

Use the above methods for Generating session file:

Python3
fromsessionimport*importsys# Take session file path as command line# argument and passed to following methodsessionFilePath=sys.argv[1]sessionGenerator(sessionFilePath)

Opening session file:

Python3
defsessionOpener(sessionFilePath):# 2.1 Verify that session file is existifsessionFilePath=="":raiseIOError('"'+sessionFilePath+'" is not exist.')# 2.2 Read the given file into "session" variablewithopen(sessionFilePath,"r",encoding="utf-8")assessionFile:session=sessionFile.read()# 2.3 Open Chrome browserbrowser=webdriver.Chrome()# 2.4 Open Web Whatsappbrowser.get("https://web.whatsapp.com/")# 2.5 Wait for Web Whatsapp to be loaded properly_wait_for_presence_of_an_element(browser,QR_CODE)# 2.6 Execute javascript in browser to inject# session by using variable "session"print("Injecting session...")browser.execute_script(INJECT_SESSION,session)# 2.7 Refresh the pagebrowser.refresh()# 2.8 Ask for user to enter any key to close browserinput("Press enter to close browser.")

Use the above methods for Opening session file:

Python3
fromsessionimport*importsys# Take session file path as command line# argument and passed to following methodsessionFilePath=sys.argv[1]sessionOpener(sessionFilePath)

We can generate a session file by using the following command:

For Generating session file:

python session_generator.py session.wa

After generating the session file then share it with someone and put that session file in the same folder where session_opener.py is located and run the following command to open the Web Whatsapp without scanning the QR code

For Opening session file:

python session_opener.py session.wa

OR open PowerShell normally without going to the folder and give absolute paths as following

For Generating session file:

python E:\share-web-whatsapp\session_generator.py E:\share-web-whatsapp\session.wa

For Opening session file:

python E:\share-web-whatsapp\session_opener.py E:\share-web-whatsapp\session.wa


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp