WhatsApp without QR code scanning everytime through Selenium WebDriver - python-3.x

Is there any whatsapp or webdriver API to access WhatsApp web without scanning QR code everytime while accessing it using selenium and chrome webdriver in python?

This is What you need. This code Read QR and store it in cookies
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
jokes = ["You don't need a parachute to go skydiving. You need a parachute to go skydiving twice.",
"This is Test Message."]
options = Options()
options.add_argument("--user-data-dir=chrome-data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)
driver.maximize_window()
driver.get('https://web.whatsapp.com') # Already authenticated
time.sleep(20)
##################### Provide Recepient Name Here ###############################
driver.find_element_by_xpath("//*[#title='MyJakartaNumber']").click()
for joke in jokes:
driver.find_element_by_xpath('//*[#id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(joke)
driver.find_element_by_xpath('//*[#id="main"]/footer/div[1]/div[3]/button/span').click()
time.sleep(10)
time.sleep(30)
driver.close()

Your "WhatsApp" and "QR Code" don't tell anything to me, however if you're testing an application which requires an extra action to sign in I don't think you will be able to perform it using Selenium as it's browser automation framework.
Web applications identify users via Cookies - special HTTP Headers containing client-side information. When you start a web browser via Selenium bindings - it kicks off a clean browser session which is not authenticated in "WhatsApp" (whatever it is)
The solutions would be in:
Authenticate in WhatsApp manually, store your browser profile somewhere and start Selenium by pointing it to the earlier storied profile folder
Authenticate in WhatsApp manually, store your browser cookies and use WebDriver.add_cookie() function to read the stored cookies into the current clean session

You can use "pywhatkit". pywhatkit is used to send messages using whatssapp web. Run:
pip install pywhatkit
and you are good to go.

Related

Unable to log into Instagram with Selenium

I am having issues with logging onto Instagram with Selenium. Having followed a number of tutorials online I am still hitting a brick wall and am unable to login to Instagram with my creds.
Any help? If you could send the full code that works for you that would be ace, as I'm feeling like I am spending way too much time on this.
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://www.instagram.com/')
prelogin_cookies =driver.find_element_by_xpath("/html/body/div[4]/div/div/button[1][text()='Only Allow Essential Cookies']")
prelogin_cookies.click()
#This works fine and clicks the cookies notification to only allow essential cookies.
driver.find_element_by_xpath('//input[#name="Phone number, username or email address"]').send_keys('USERNAME')
driver.find_element_by_xpath('//input[#type="Password"]').send_keys('password')
driver.find_element_by_xpath('//input[#type="password"]').submit()
#Alternatives I have tried - still unable to locate paths to login buttons
# password_input = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input[text()='Password']")
# username_input = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input[text()='Phone number, username or email address']")
# username_input = driver.find_element_by_css_selector('class._2hvTZ pexuQ zyHYP')

extract and save whatsapp session from chrome browser local storage and then use it on another device

To avoid mark as duplicate then I put some source where I've read :
Python and Selenium - Reboot program and reuse same browser session
Python and Selenium - Reuse a whatsapp web session
Currently I want to make python selenium application so that it could send my message to my customer. On those 2 articles on above say if possible to use local storage to keep logged in into web.whatsapp.com after scan qr-code once.
this is my code :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import sys
from selenium.webdriver.chrome.options import Options
chromepath = r'E:\chromedriver\chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=chromepath, chrome_options=options)
This code worked well, so I can open web.whatsapp.com with login at once, and then use it to send message.
Q : Is possible to save whatsapp session from chrome local storage to database eg : mysql or etc so that I can use it to make whatsapp web keep logged in although with different device / computer.
In other word is possible to change the value of :
user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data
with the value of web.whatsapp.com session that stored in my database?
Note : if not possible, please tell me another solution so that I still can use it to access whatsapp account in another device without copy user data folder and then move to new device.
Thanks
if using linux or mac go to your terminal and type :
chromium --remote-debugging-port=9250
where chromium is the browser and 9250 is a port number which you can choose and then in your selenium file while making a selenium object
opts=Options()
opts.add_experimental_option("debuggerAddress","localhost:9250")
driver=webdriver.Chrome(options=opts)
and this shall open your browser with every websites logged in.

Selenium - Log in with URL failed

I am new to selenium am having trouble authenticating to a website. I was provided a link that automatically authenticates me and redirects me to a homepage of a the site. When I try running this code below I am just redirected to the login page and it says authentication failed(Changed the link for obvious reasons). I use the same link on my browser and it logs me in. Am I missing an option that I have to specify somewhere?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome('/home/ec2-user/test/chromedriver',options=chrome_options)
browser.get('https://examplelink.com/#/login?token=aewoijfadsklnfwjeojwfqoj234ihg')

How to use Chrome Webdriver cache for Whatsapp Web?

I created a script to send messages in an automated way through the WhatsApp Web.
However, I am not able to save the cache, containing the QR Code of Authentication, to the Chrome Web Driver through Selenium.
Each time the browser is opened to send a new message, reading the QR Code is requested again.
I'm using Ubuntu and Google Chrome on version 74.0.3729.157
What I've tried so far:
options = Options()
options.add_argument("--disable-extensions")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--user-data-dir=./User_Data')
options.add_argument("--test-type")
driver = webdriver.Chrome(options=options, executable_path="/usr/bin/chromedriver")
driver.get(url)
time.sleep(10)
python_button = driver.find_element_by_id("action-button")
python_button.click()
time.sleep(30)
python_button2 = driver.find_element_by_css_selector("span[data-icon='send']")
python_button2.click()
Content of User_Data folder:
What's missing so I do not have to scan the QR code every time?

selenium driver not saving the webpage content

Below code is creating an empty task_list.html file. It is not saving the full html content file , which is what i want . How to fix it?
from selenium import webdriver
import codecs
driver = webdriver.Firefox()
html = driver.page_source
driver.get("<our_internal_work_website>")
with open(r"C:\Users\task_list.html", "wb") as f:
f.write(html.encode('utf-8'))
Few points:
I am brand new to Python, just started few days back and troubleshooting issues as they come by searching on stackoverflow.
I am running this code on interactive shell on Powershell in Windows 10.
I manually enter the login credentials when opens . I have still not reached expertise to be able to put login details automatically.
I do not have html/css knowledge , so will google around in order to troubleshoot.
I want to use beautiful soup in order to parse the html , but it also requires login credentials and i do not know where to put the login credentials because it doesnt explicitly open the "Firefox" browser. Due to that , for now i am working with Selenium, because it can open the Firefox browser.

Resources