Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chat bot
#1
I need a help with this code:
from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv

message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 555182073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')

def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)

def is_connected():
    try:
        # connect to the host -- tells us if the host is actually
        # reachable
        socket.create_connection(("www.google.com", 80))
        return True
    except :
        is_connected()
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second

def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    try:
        #driver.switch_to_alert().accept()#alterar a linha ou comenta#  
        driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    except Exception as e:
        pass

    try:
        element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
        txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
        global no_of_message
        for x in range(no_of_message):
            txt_box.send_keys(text)
            txt_box.send_keys("\n")

    except Exception as e:
        print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    try:
        send_whatsapp_msg(moblie_no, message_text)

    except Exception as e:
        sleep(10)
        is_connected()
tje list in line 13 return me an error in second phone numbe...this number is a invalid numeber
Reply
#2
Thumbs Up 
Ok thanks

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv
 
message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')
 
def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)
 
def is_connected():
    try:
        # connect to the host -- tells us if the host is actually
        # reachable
        socket.create_connection(("www.google.com", 80))
        return True
    except :
        is_connected()
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second
 
def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    try:
        #driver.switch_to_alert().accept()#alterar a linha ou comenta#  
        driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    except Exception as e:
        pass
 
    try:
        element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
        txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
        global no_of_message
        for x in range(no_of_message):
            txt_box.send_keys(text)
            txt_box.send_keys("\n")
 
    except Exception as e:
        print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    try:
        send_whatsapp_msg(moblie_no, message_text)
 
    except Exception as e:
        sleep(10)
        is_connected()
here the error

Error:
[error]Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> == RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == invailid phone no :5551982073305 >>>
[/error]

Output:
DevTools listening on ws://127.0.0.1:60671/devtools/browser/a36c5c82-0e98-4945-b292-f476b1d75a3d
Reply
#3
Never use general, all catch exception like this, at least while you develope. Be as specific as possible exactly which exceptions you want to handle.
Now remove temporary the try/except in order to see what real error is
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Sorry for this..but i don´t know where the error was occuried. Well I did like you told me to do: I removed all try´s and excepts

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv

message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5559182073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')

def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)

def is_connected():
    
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second

def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
Error:
[error]driver
= webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
Error:
expected an indent block
[/error]
Reply
#5
what I mean is, try following:

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv
  
message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')
  
def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)
  
# def is_connected():
#     try:
#         # connect to the host -- tells us if the host is actually
#         # reachable
#         socket.create_connection(("www.google.com", 80))
#         return True
#     except :
#         is_connected()

driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second
  
def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    # try:
        #driver.switch_to_alert().accept()#alterar a linha ou comenta#  
    driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    # except Exception as e:
    #     pass
  
    # try:
    element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
    txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
    global no_of_message
    for x in range(no_of_message):
        txt_box.send_keys(text)
        txt_box.send_keys("\n")
  
    # except Exception as e:
    #     print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    # try:
    send_whatsapp_msg(moblie_no, message_text)
    # except Exception as e:
    #     sleep(10)
    #     is_connected()
I removed the is_connected() function. Also usisng globals is bad practice. just pass no_of_message as argument (I didn't implement this)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv
   
message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')
   
def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)
   
 
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second
   
def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    # try:
        #driver.switch_to_alert().accept()#alterar a linha ou comenta#  
    driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    # except Exception as e:
    #     pass
   
    # try:
    element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
    txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
    global no_of_message
    for x in range(no_of_message):
        txt_box.send_keys(text)
        txt_box.send_keys("\n")
   
    # except Exception as e:
    #     print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    # try:
    send_whatsapp_msg(moblie_no, message_text)
    # except Exception as e:
    #     sleep(10


Output:
DevTools listening on ws://127.0.0.1:61710/devtools/browser/85cbcdcd-b841-4966-b32a-e55094b3f384
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py ==

Error:
== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == invailid phone no :5551982073305 >>>

[quote='akanowhere' pid='99767' dateline='1576080912']
Sorry for this..but i don´t know where the error was occuried. Well I did like you told me to do: I removed all try´s and excepts

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv

message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5559182073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')

def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)

def is_connected():
    
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second

def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
Error:
[error]driver
= webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
Error:
expected an indent block
[/error]
[/quote]

[quote='buran' pid='99775' dateline='1576092474']
what I mean is, try following:

python]from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv

message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')

def element_presence(by,xpath,time):
element_present = EC.presence_of_element_located((By.XPATH, xpath))
WebDriverWait(driver, time).until(element_present)


driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second

def send_whatsapp_msg(phone_no,text):
driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
# try:
#driver.switch_to_alert().accept()#alterar a linha ou comenta#
driver.switch_to_alert.accept()
#driver.switch_to_alert().accept()
# except Exception as e:
# pass

# try:
element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
global no_of_message
for x in range(no_of_message):
txt_box.send_keys(text)
txt_box.send_keys("\n")

# except Exception as e:
# print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
# try:
send_whatsapp_msg(moblie_no, message_text)
# except Exception as e:
# sleep(10[/python]


Output:
DevTools listening on ws://127.0.0.1:61710/devtools/browser/85cbcdcd-b841-4966-b32a-e55094b3f384
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py ==

Error:
== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == invailid phone no :5551982073305 >>>
Reply
#7
Thanks a lot for your help. As you recomend I removed the is_connected() function as bellow and didt´n work in
second phone number in the list again. The same error persists

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv
   
message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')
   
def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)
   
 
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second
   
def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    # try:
        #driver.switch_to_alert().accept()#alterar a linha ou comenta#  
    driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    # except Exception as e:
    #     pass
   
    # try:
    element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
    txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
    global no_of_message
    for x in range(no_of_message):
        txt_box.send_keys(text)
        txt_box.send_keys("\n")
   
    # except Exception as e:
    #     print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    # try:
    send_whatsapp_msg(moblie_no, message_text)
    # except Exception as e:
    #     sleep(10)


Output:
DevTools listening on ws://127.0.0.1:61710/devtools/browser/85cbcdcd-b841-4966-b32a-e55094b3f384
Error:
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> == RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == [error]== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == invailid phone no :5551982073305 >>>
Reply
#8
Obviously it is not possible to get this error with that code - because line 43 is commented out (or should be) - so it could not possibly print invailid phone no :5551982073305
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Hi again...listem i have removed de all comments to rerun again and it doesn´t work.
Could you have a whatsapp number for contact...i could show you the problem by cam...
I think this will be more efective!!!


from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import socket
#import csv
   
message_text='Ola essa é uma mensagem automatizada de Whats App.' # message
no_of_message = 1 # no. of time
moblie_no_list = [5551981440050, 5551982073305] # list of phone number
#moblie_no_list= open('entrada.csv', 'r')
   
def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)
   

 
driver = webdriver.Chrome(executable_path="D:\Programas\Aulas\scripts\Projetos\whatsautomatico\chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second
   
def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    try:
         
        driver.switch_to_alert.accept()
        #driver.switch_to_alert().accept()
    except Exception as e:
         pass
   
    try:
        element_presence(By.XPATH,'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]',30)
        txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
        global no_of_message
        for x in range(no_of_message):
            txt_box.send_keys(text)
            txt_box.send_keys("\n")
       
    except Exception as e:
         print("invailid phone no :"+str(phone_no))
for moblie_no in moblie_no_list:
    try:
        send_whatsapp_msg(moblie_no, message_text)
    except Exception as e:
         sleep(10)
The error apper in python shell:

e
Error:
xcept Exception as e: print("invailid phone no :"+str(phone_no)) Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> == RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == [error]== RESTART: D:\Programas\Aulas\scripts\Projetos\whatsautomatico\Aut0001.py == invailid phone no :5551982073305 >>>
Reply
#10
It's impossible to tell what the problem is. You are catching every possible exception on line 43, and then printing a message about phone numbers. But you don't know what the exception was. Furthermore, you catch any exceptions on line 32 and then just ignore them. It could be that line 32 caused a problem, your program ignored it, and then that caused another problem on lines 36 to 41.

Make a version of that function with no try/except blocks, and post the full error message you get.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question download watsapp chat Orliv 1 1,760 Jan-26-2021, 07:01 PM
Last Post: Larz60+
  I am trying to send message in youtube live chat stylohunk 1 2,590 Jun-08-2019, 04:06 PM
Last Post: SheeppOSU
Sad Twitch Chat Bot - Disconnects after 10 minutes sp4wny 0 3,895 Dec-20-2017, 08:28 AM
Last Post: sp4wny
  Host and port problems for UDP chat server YevesDraco 2 3,820 May-07-2017, 04:35 PM
Last Post: YevesDraco
  How to use a UDP server and client to setup a chat abrew132 2 3,764 Mar-20-2017, 06:22 PM
Last Post: abrew132
  Scraping Content from Google Chat ATXpython 2 5,423 Oct-26-2016, 06:07 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020