I'm trying to make a little Facebook group auto post script using python and selenium
First of all I'm uploading 3/4 photos using the following code:
l = driver.find_elements_by_tag_name('input')
for g in l:
print(g)
try:
if g == driver.find_element_by_xpath("//input[#type='file']"):
print("Found")
logging.debug("Found input for image uploading")
g.send_keys(
'/var/www/html/v1/insta-post/AutoPostFB/images/0.jpg \n/var/www/html/v1/insta-post/AutoPostFB/images/1.jpg \n/var/www/html/v1/insta-post/AutoPostFB/images/2.jpg')
print("File/s Uploaded")
logging.debug("Images uploaded")
time.sleep(5)
# break
except:
print("Element not found after upload")
logging.debug("Element for upload not found")
Then I push the post text, since the textbox is already on focus
actions = ActionChains(driver)
actions.send_keys(info)
actions.perform()
time.sleep(1)
The issue is that every space in the info variable is abnormally converted into a return key
so if the text is like
info = "HEI HOW'S IS GOING?"
what I get in the textbox is
IS
GOING
HEY
HOW'S
Someone can help me out? I've tried like everything
Related
def Item_Finder():
item_finder = driver.find_element(By.XPATH, "XPATH HERE").text
item_finder = re.sub('[%+]', '', item_finder)
item_finder = float(item_finder)
return item_finder
while Item_Finder() <= 5:
driver.find_element(By.XPATH, "XPATH HERE").click()
else:
Item_Finder()
print("No item found. Retrying...")
Cant seem to get this while loop working it only runs once. Code just looks at item markup on peoples listings . First post on here aswell not to sure how to get the indentations to show but they are there. Any help appreciated only recently started learning.
I want to create a program, that can read all the messages from my whatsApp and print them to the screen using python.
In order to do that I tried using the whatsapp-web library https://pypi.org/project/whatsapp-web/.
But when i tried to run their code example I got a timeout error
this is the code
import time
from selenium import webdriver
from simon.accounts.pages import LoginPage
from simon.header.pages import HeaderPage
from simon.pages import BasePage
# Creating the driver (browser)
driver = webdriver.Firefox()
driver.maximize_window()
login_page = LoginPage(driver)
login_page.load()
login_page.remember_me = False
time.sleep(7)
base_page = BasePage(driver)
base_page.is_welcome_page_available()
base_page.is_nav_bar_page_available()
base_page.is_search_page_available()
base_page.is_pane_page_available()
base_page.is_chat_page_available()
# 3. Logout
header_page = HeaderPage(driver)
header_page.logout()
# Close the browser
driver.quit()
and this is the error
base_page.is_welcome_page_available()
File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 18, in wrapper
return func(*args, **kwargs)
File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 51, in is_welcome_page_available
if self._find_element(WelcomeLocators.WELCOME):
File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 77, in _find_element
lambda driver: self.driver.find_element(*locator))
File "D:\zoom\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Your code is not completed to find error or correct.
Because the error is coming from the imported page.
Try by increasing the time limit to load the page,
use time.sleep(15)
You can try to automate the WhatsApp web by yourself without using pip of WhatsApp-web. because I guess that is not updated.
The code is giving error because of WhatsApp web has changed its elements classes names. Because of that the code section is not able to find welcome page in given time limit. So the program execution breaks
I have done the same without using WhatsApp-web pip.
I hope it will work for you.
A complete code reference Example :
from selenium import webdriver
import time
# You can use any web-browser which supported by selenium and which can run WhatsApp web.
# For using GoogleChrome
web_driver = webdriver.Chrome("Chrome_Driver_Path/chromedriver.exe")
web_driver.get("https://web.whatsapp.com/")
# For using Firefox
# web_driver = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
# web_driver.get("https://web.whatsapp.com/")
time.sleep(25) # For scan the qr code
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
print("Continuing...")
elif confirm == 0:
web_driver.close()
exit()
else:
print("Sorry Please Try again")
web_driver.close()
exit()
while True:
unread_chats = web_driver.find_elements_by_xpath("// span[#class='_38M1B']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
# Open each chat using loop and read message.
for chat in unread_chats:
chat.click()
time.sleep(2)
# For getting message to perform action
message = web_driver.find_elements_by_xpath("//span[#class='_3-8er selectable-text copyable-text']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.
for i in message:
try:
print("Message received : " + str(i.text))
# Here you can use you code to perform action according to your need
except:
pass
Please make sure that the indentation is equal in code blocks if you are copying it.
Can following link for more info about WhatsApp web using python.
https://stackoverflow.com/a/68288416/15284163
I am developing WhatsApp bot using python.
For contribution you can contact at : anurag.cse016#gmail.com
Please give a star on my https://github.com/4NUR46 If this Answer helps you.
I've found a solution (example at this link How to line break in WhatsApp with Selenium when sending a message?
But I've a problem sending Multiline Message with WhatsApp using Python and Selenium.
This is My code :
message = excel_data['Message'][msg]
# Locate search box through x_path
search_box = '//*[#id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))
# Clear search box if any contact number is written in it
person_title.clear()
# Send contact number in search box
person_title.send_keys(str(excel_data['Contact'][count]))
count = count + 1
msg=msg+1
# Wait for 2 seconds to search contact number
time.sleep(2)
try:
# Load error message in case unavailability of contact number
element = driver.find_element_by_xpath('//*[#id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
# Format the message from excel sheet
message = message.replace('{customer_name}', column)
person_title.send_keys(Keys.ENTER)
actions = ActionChains(driver)
actions.send_keys(message)
actions.send_keys(Keys.ENTER)
actions.perform()
I've a file excel with 2 column : 1° Column Phone Number and 2° Column the message
All work well if message is a single message.
If message is on multi line doesn't work.
Ex.:
Message =
Hello
Gundam How are you?
I'm well
WhataApp send 3 message :
First with Hello
Second with Gundam How are you?
Third with I'well
I need all in One message in multiline
Could you help me modifying my code ?
I tried adding this but doesn't work:
ActionChains(driver).send_keys(message).perform()
ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()
ActionChains(driver).send_keys(Keys.RETURN).perform()
Thanks a lot for your help
Use selenium Keys:
from selenium.webdriver.common.keys import Keys
Then:
val="text\text"
val =val.replace("\n",(Keys.SHIFT+Keys.ENTER))
just replace '\n' or '\r\n' with (Keys.SHIFT+Keys.ENTER)
so in your case:
First check what is the line end character
print((message).encode("unicode_escape"))
Then replace that with Keys.shift+enter
message=message.replace("\n",(Keys.SHIFT+Keys.ENTER))
You can directly use the unicode charactes:
elem.send_keys("first\ue008\ue007second")
following functions hopefully solve your purpose. Actually in whatsapp, pressing Enter Key will send the message immediately so won't work for multiline. for multiline shift + enter required to press at same time.
Selenium ActionChains method used to overcome the problem.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webd`enter code here`river.common.action_chains import ActionChains
def wa_msg_send(driver, msg):
messages = msg.split('\n')
for line in messages:
ActionChains(driver).send_keys(line).perform()
ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()
else:
ActionChains(driver).send_keys(Keys.RETURN).perform()
functions argument driver can be created using following line & msg is your full multiline message.
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
Please check and let me know if not worked.
In the whatsapp web '\n' works an 'Enter' key
And 'Enter' key works as send message on whatsapp web.
Because of that your multi-line message is getting send in mult-parts (or as multiple message)
For avoid it you can change '\n' with shift+Enter Key.
You can use the following way to send message with break line in a single message
from selenium.webdriver.common.keys import Keys
#You have to install selenium for keys or can choose any other library which be suitable for you.
br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT) # Assigning the keys for break line
## You can use any one way from the following.
message = f"Dear Student,{br}Please send your report{br}Thank you for your attention"
####################### Or #########################
# message = "Dear Student,{0}Please send your report{0}Thank you for your attention".format(br)
####################### Or #########################
# message = "Dear Student," + br + "Please send your report" + br + "Thank you for your attention"
Or, You can use escape character as following example
message = """Hello \
Gundam How are you? \
I'm well """
I have already explain multiple line breaking ways on a question.
You can check my Answer here.
Thank you
I'm building a bot that logs into zoom at specified times and the links are being obtained from whatsapp. So i was wondering if it is was possible to retrieve those links from whatsapp directly instead of having to copy paste it into python. Google is filled with guides to send messages but is there any way to READ and RETRIEVE those messages and then manipulate it?
You can, at most, try to read WhatsApp messages with Python using Selenium WebDriver since I strongly doubt that you can access WhatsApp APIs.
Selenium is basically an automation tool that lets you automate tasks in your browser so, perhaps, you could write a Python script using Selenium that automatically opens WhatsApp and parses HTML information regarding your WhatsApp web client.
First of all, we mentioned Selenium, but we will use it only to automate the opening and closing of WhatsApp, now we have to find a way to read what's inside the WhatsApp client, and that's where the magic of Web Scraping comes is hand.
Web scraping is a process of extracting data from a website, in this case, the data is represented by the Zoom link you need to automatically obtain, while the web site is your WhatsApp client. To perform this process you need a way to extract (parse) information from the website, to do so I suggest you use Beautiful Soup, but I advise you that a minimum knowledge of how HTML works is required.
Sorry if this may not completely answer your question but this is all the knowledge I have on this specific topic.
You can open WhatsApp on browser using https://selenium-python.readthedocs.io/ in Python.
Selenium is basically an automation tool that lets you automate tasks in your browser so, perhaps, you could write a Python script using Selenium that automatically opens WhatsApp and parses HTML information regarding your WhatsApp web client.
I learn and use code from "https://towardsdatascience.com/complete-beginners-guide-to-processing-whatsapp-data-with-python-781c156b5f0b" this site. Go through the details written on mentioned link.
You have to install external python library "whatsapp-web" from this link --- "https://pypi.org/project/whatsapp-web/". Just type in command prompt / windows terminal by "python -m pip install whatsapp-web".
It will show result ---
python -m pip install whatsapp-web
Collecting whatsapp-web
Downloading whatsapp_web-0.0.1-py3-none-any.whl (21 kB)
Installing collected packages: whatsapp-web
Successfully installed whatsapp-web-0.0.1
You can read all the cookies from whatsapp web and add them to headers and use the requests module or you can also use selenium with that.
Update :
Please change the xpath's class name of each section from the current time class name of WhatsApp web by using inspect element section in WhatsApp web to use the following code. Because WhatsApp have changed its element's class names.
I have tried that in creating a WhatsApp bot using python.
But there are still many bugs because of I am also beginner.
steps based on my research :
Open browser using selenium webdriver
Login on WhatsApp using qr code
If you know from which number you are going to received the meeting link then use this step otherwise check the following process mention after this process.
Find and open the chat room where you are going to received zoom meeting link.
For getting message from known chat room to perform action
#user_name = "Name of meeting link Sender as in your contact list"
Example :
user_name = "Anurag Kushwaha"
#In above variable at place of `Anurag Kushwaha` pass Name or number of Your Teacher
# who going to sent you zoom meeting link same as you have in your contact list.
user = webdriver.find_element_by_xpath('//span[#title="{}"]'.format(user_name))
user.click()
# For getting message to perform action
message = webdriver.find_elements_by_xpath("//span[#class='_3-8er selectable-text copyable-text']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.
for i in message:
try:
if "zoom.us" in str(i.text):
# Here you can use you code to preform action according to your need
print("Perform Your Action")
except:
pass
If you do not know by which number you are going to received the link.
Then you can get div class of any unread contact block and get open all the chat room list which are containing that unread div class.
Then check all the unread messages of open chat and get the message from the div class.
When you don't know from whom you gonna received zoom meeting link.
# For getting unread chats you can use
unread_chats = webdriver.find_elements_by_xpath("// span[#class='_38M1B']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
# Open each chat using loop and read message.
for chat in unread_chats:
chat.click()
# For getting message to perform action
message = webdriver.find_elements_by_xpath("//span[#class='_3-8er selectable-text copyable-text']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.
for i in messge:
try:
if "zoom.us" in str(i.text):
# Here you can use you code to preform action according to your need
print("Perform Your Action")
except:
pass
Note : In the above code 'webdriver' is the driver by which you open web.whatsapp.com
Example :
from selenium import webdriver
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp.com")
# This wendriver variable is used in above code.
# If you have used any other name then please rename in my code or you can assign your variable in that code variable name as following line.
webdriver = your_webdriver_variable
A complete code reference Example :
from selenium import webdriver
import time
webdriver = webdriver.Chrome("ChromePath/chromedriver.exe")
webdriver.get("https://web.whatsapp.com")
time.sleep(25) # For scan the qr code
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
print("Continuing...")
elif confirm == 0:
webdriver.close()
exit()
else:
print("Sorry Please Try again")
webdriver.close()
exit()
while True:
unread_chats = webdriver.find_elements_by_xpath("// span[#class='_38M1B']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
# Open each chat using loop and read message.
for chat in unread_chats:
chat.click()
time.sleep(2)
# For getting message to perform action
message = webdriver.find_elements_by_xpath("//span[#class='_3-8er selectable-text copyable-text']")
# In the above line Change the xpath's class name from the current time class name by inspecting span element
# which containing received text message of any chat room.
for i in messge:
try:
if "zoom.us" in str(i.text):
# Here you can use you code to preform action according to your need
print("Perform Your Action")
except:
pass
Please make sure that the indentation is equal in code blocks if you are copying it.
Can read my another answer in following link for more info about WhatsApp web using python.
Line breaks in WhatsApp messages sent with Python
I am developing WhatsApp bot using python.
For contribution you can contact at : anurag.cse016#gmail.com
Please give a star on my https://github.com/4NUR46 If this Answer helps you.
Try This Its A bit of a hassle but it might work
import pyautogui
import pyperclip
import webbrowser
grouporcontact = pyautogui.locateOnScreen("#group/contact", confidence=.6) # Take a snip of the group or contact name/profile photo
link = pyperclip.paste()
def searchforgroup():
global link
time.sleep(5)
webbrowser.open("https://web.whatsapp.com")
time.sleep(30)#for you to scan the qr code if u have done it then u can edit it to like 10 or anything
grouporcontact = pyautogui.locateOnScreen("#group/contact", confidence=.6)
x = grouporcontact[0]
y = grouporcontact[1]
if grouporcontact == None:
#Do any other option in my case i just gave it my usual link as
link = "mymeetlink"
else:
pyautogui.moveTo(x,y, duration=1)
pyautogui.click()
# end of searching group
def findlink():
global link
meetlink = pyautogui.locateOnScreen("#", confidence=.6)#just take another snap of a meet link without the code after the "/"
f = meetlink[0]
v = meetlink[1]
if meetlink == None:
#Do any other option in my case i just gave it my usual link as
link = "mymeetlink"
else:
pyautogui.moveTo(f,v, duration=.6)
pyautogui.rightClick()
pyautogui.moveRel(0,0, duration=2) # You Have to play with this it basically is considered by your screen size so just edit that and edit it till it reaches the "Copy Link Address"
pyautogui.click()
link = pyperclip.paste()
webbrowser.open(link) # to test it out
So Now You Have It Have To Install pyautogui, pyperclip
and just follow the comments in the snippet and everything should work :)
I'm using Raspberry Pi with Selenium and PhantomJS. I'm trying to verify the captcha from Google reCaptcha 2.0 but it said that the captcha is not correct (solve more). It always happens to 100% and I've never got a passed captcha from Selenium.
See, as example on this picture:
Then when I click on "Verify"-Button, then this happend:
My python code:
self.driver.switch_to.frame(capcthaframe)
picturetable = self.driver.find_element_by_css_selector('.rc-imageselect-table-3')
images = []
for row in picturetable.find_elements_by_tag_name('tr'):
for col in row.find_elements_by_tag_name('td'):
images.append(col.find_element_by_tag_name('img'))
if images == []:
self.fail("Found no captcha images")
return -1
print "[*] Got answer : " + str(numbers)
for number in numbers:
index = int(number)-1
images[index].click()
print '[+] clicked on image '+str(index+1)
self.driver.save_screenshot('res.png')
time.sleep(1)
verifybutton = self.driver.find_element_by_id('recaptcha-verify-button')
verifybutton.click()
print "[*] Clicked verify button"
time.sleep(10)
I don't think that my code is wrong... So, have anybody a idea what's wrong with this reCaptcha error?