Interaction with whatsapp web using python - python

Is it possible to make a program in python in which the program will look for a message on WhatsApp sent by a particular person on a group(which I have pinned for convenience) at a particular time and then it will print the message sent by that person?
Well if you don't understand my question let me give you an example:
Suppose there is a group named ABCD which is pinned on WhatsApp web(which means it is at the top)
It consists of 4 people - A,B,C,D
I want the program to print the message sent by C at time 13:05 in that group
Is this thing possible on python?
I can use any module like selenium or even pyautogui

Yes, it is possible through Selenium without using any kind of API.
First we will import required modules:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
Now we will launch Whatsapp Web:
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com")
You can use other browsers, for this example we will be using Chrome.
Now we will locate the Search or Start New Chat button using XPath and add a webdriver wait for it:
search_button = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath("//input[#title='Search or start new chat']"))
Now we will click it and add a little sleep:
search_button.click()
time.sleep(2)
Now we will Send the name of the Person to contact into the text area:
search_button.send_keys("A")
time.sleep(2)
Now we will send a message
input_box = driver.find_element_by_xpath(r'//div[#class="_2S1VP copyable-text selectable-text"][#contenteditable="true"][#data-tab="1"]')
time.sleep(2)
input_box.send_keys("Hello" + Keys.ENTER)
time.sleep(2)
You can using Chrome inspect more and do more things.

Related

How to write a Python Selenium script that can check a website for new information?

Im new to programming and as a first project I wanted to write a script in Python with Selenium that can check my schools website and give me a notification when I receive a new grade. I already wrote a code in Python with Selenium that logs in to my account and goes to my grades. But what I don't know is how to let it constantly check for new grades and that when there is a new one, it sends me a notification to my iPhone via SMS, Whatsapp, email or another way. I really hope that someone has time to answer my question! Thanks a lot!
Here is the code I wrote so far (I replaced sensitive information with ******* so that you can't see it ;)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "/Users/***********/Documents/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://minkema.magister.net/magister/#/cijfers")
search = driver.find_element_by_id("username")
search.send_keys("******")
search.send_keys(Keys.RETURN)
try:
password_login = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "rswp_password"))
)
except:
driver.quit()
password_inloggen = driver.find_element_by_id("rswp_password")
password_inloggen.send_keys("*********")
password_inloggen.send_keys(Keys.RETURN)
First, you'll need to use some scheduler like a cronjob to check the site periodically
then you'll have to use a database to store the grades that are already sent to you so it won't be sent every time the script runs.
and as a notification tool, I would recommend using a Telegram bot.

How to fill a textfield and pressing a button multiple times in order in Selenium?

I want to automatically fill out a form and then click the Subscribe button multiple times on this certain website. But somehow the behaviors are inconsistent and the actions aren't performed in a correct order.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome("chromedriver.exe")
browser.get("https://www.facen.co/")
emails = ["mikhail67#gmail.com", "konovalov878#gmail.com", "miruki451#gmail.com"]
# send a list of random emails
for email in emails:
sub_textfield = browser.find_element_by_id("comp-ka58ijvyinput")
sub_button = browser.find_element_by_id("comp-ka58ijw9link")
ActionChains(browser).move_to_element(sub_textfield).send_keys(email).move_to_element(sub_button).click().perform()
I tried to keep order of executions using ActionChains, but it doesn't seem to be working. What could be the problem?
Try adding some delays using the time module
and also DO NOT execute the last line as a whole thing in a single line
use name spacing and put them in different lines and add some delays so this will give the computer some time to know whats happening and let the thing load
That could be the problem sometimes that the code has run faster than the page has loaded
Typically something like this would not need to use ActionChains. It doesn't sound like you have a timing issue but I would add a wait to the first line in your loop just to be safe.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome("chromedriver.exe")
browser.get("https://www.facen.co/")
emails = ["mikhail67#gmail.com", "konovalov878#gmail.com", "miruki451#gmail.com"]
# send a list of random emails
for email in emails:
WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "comp-ka58ijvyinput")).send_keys(email)
browser.find_element_by_id("comp-ka58ijw9link").click()

Select from dropdown Selenium

New to Selenium. I'm trying to get it so that I can enter a name into a search box and then click on the correct name.
I have managed to write some code to go to a website and then enter what I want and press the search button. The problem is that it then shows a list of items so I'd have to click again.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.marketscreener.com')
sleep(6)
text_box = driver.find_element_by_css_selector('#autocomplete')
text_box.send_keys(index)
#select.select_by_index(1)
driver.find_element_by_xpath("//*[#id='recherche_menu']/table/tbody/tr[1]/td/button/img").click()
When I enter the name and don't click It presents a dropdown list so I am trying to select the first item from the dropdown as that is usually what I will want.
I attempted this by using the commented out select.select_by_index code line. But it doesn't quite work.
I just tried also using text_box.send_keys(Keys.DOWN, Keys.RETURN) to move down into the dropdown field, but this doesn't work and just returns the same as what I currently get from clicking.
To be clear what I mean is that currently the code will return this:
But I want it to go straight to the Amazon page so it will return this:
Any help appreciated.
Thanks
This page use many JavaScript to listen the action of the mouse and the key.If you didn't focus on the searchbox,the dropdown will be disappeared.(and you couldn't find it in the source code).
Try to use ActionChains,this works fine on my PC:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome()
driver.get('https://www.marketscreener.com')
action = ActionChains(driver)
sleep(4)
text_box = driver.find_element_by_css_selector('#autocomplete')
action.move_to_element(text_box)
action.click(text_box)
action.send_keys(index)
action.perform()
sleep(1)
driver.find_element_by_xpath('//*[#id="AC_tRes"]/li[1]').click()
If the result couldn't show you,maybe you need to increase the time of sleep().

selenium python for creating a instagram login liker bot

I am using selenium for python to create a simple liker bot for instagram. The idea is to like the first photo of a tag (in this example is "sunset"). It correctly selects the first photo but does not insert a like.
The code is as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
import User_data
chrome_options=Options()
chrome_options.add_argument('--lang=en')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get("https://www.instagram.com/accounts/login/")
sleep(1)
browser.find_element_by_name("username").send_keys(User_data.username)
browser.find_element_by_name("password").send_keys(User_data.password)
sleep(1)
browser.find_element_by_xpath('//*[#id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button').click()
sleep(1)
browser.get("https://www.instagram.com/explore/tags/sunset/")
sleep(1)
browser.find_element_by_xpath("//article/div[2]/div/div/div/a/div/div[2]").click()
sleep(1)
browser.find_element_by_xpath("//button/span[contains(#class. 'glyphsSpriteHeart__outline__24__grey_9 u-__7') ]").click()
The reason you're unable to Log In is because your code is unable to find the element by xpath you specified. That is probably because the xpath that you provide is not supported.
I have found a workaround for this where I use the function find_elements_by_tag_name() which returns me the list of buttons, and with simple iteration I found that the second button returned corresponds to the Log In button on Instagram. So here's a working code that you will find useful.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
import User_data
chrome_options=Options()
chrome_options.add_argument('--lang=en')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get("https://www.instagram.com/accounts/login/")
sleep(1)
browser.find_element_by_name("username").send_keys(User_data.username)
browser.find_element_by_name("password").send_keys(User_data.password)
sleep(2)
buttons = browser.find_elements_by_tag_name('button')
for button_element in buttons:
print(button_element.text) #- This will print the text of the buttons present
This will print you the text fields of all the buttons:
Output:
Show
Log in
Log in with Facebook
These are the three buttons that come in my output, and since I have to click on the button with Log in in it's text, which is the second element from my list buttons[], I use index 1 to denote that and click it.
buttons[1].click()

How can I make Selenium/Python wait for the user to login before continuing to run?

I'm trying to run a script in Selenium/Python that requires logins at different points before the rest of the script can run. Is there any way for me to tell the script to pause and wait at the login screen for the user to manually enter a username and password (maybe something that waits for the page title to change before continuing the script).
This is my code so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import unittest, time, re, getpass
driver = webdriver.Firefox()
driver.get("https://www.facebook.com/")
someVariable = getpass.getpass("Press Enter after You are done logging in")
driver.find_element_by_xpath('//*[#id="profile_pic_welcome_688052538"]').click()
Use WebDriverWait. For example, this performs a google search and then waits for a certain element to be present before printing the result:
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://www.google.com')
wait = ui.WebDriverWait(driver, 10) # timeout after 10 seconds
inputElement = driver.find_element_by_name('q')
inputElement.send_keys('python')
inputElement.submit()
results = wait.until(lambda driver: driver.find_elements_by_class_name('g'))
for result in results:
print(result.text)
print('-'*80)
wait.until will either return the result of the lambda function, or a selenium.common.exceptions.TimeoutException if the lambda function continues to return a Falsey value after 10 seconds.
You can find a little more information on WebDriverWait in the Selenium book.
from selenium import webdriver
import getpass # < -- IMPORT THIS
def loginUser():
# Open your browser, and point it to the login page
someVariable = getpass.getpass("Press Enter after You are done logging in") #< THIS IS THE SECOND PART
#Here is where you put the rest of the code you want to execute
THEN whenever you want to run the script, you type loginUser() and it does its thing
this works because getpass.getpass() works exactly like input(), except it doesnt show any characthers ( its for accepting passwords and notshowing it to everyone looking at the screen)
So what happens is your page loads up. then everything stops, Your user manually logs in, and then goes back to the python CLI and hits enter.

Categories