Hi!
Use case
Sending out automated WhatsApp messages using pythonanywhere. Step by step logic below:
- non-coders write on a gsheet the phone numbers to which we should send out the messages
- the gsheet data is read (using gspread in pythonanywhere)
- open whatsapp url to send out the messages in bulk
I have a code using selenium running on my machine that opens the web whatsapp url, finds the needed elements on the web site, and sends the messages to the gsheets phone numbers - find below a snippet from that part of the code that I am using on my machine:
driver = webdriver.Chrome('MY PATH')
driver.implicitly_wait(10)
waiter = WebDriverWait(driver, 10)
def send_whatsapp_message():
global driver
driver.get('https://web.whatsapp.com/')
waiter.until(EC.title_is("WhatsApp"))
waitCounter = 0
while 1:
try:
waiter.until(EC.presence_of_element_located((By.XPATH, "//canvas[@aria-label='Scan me!']")))
waitCounter+=1
if waitCounter%1000 == 0:
print("Waiting for user to log in...", 'WARNING')
except:
print("Logged in to WhatsApp")
break
for entry in data:
driver.find_element_by_xpath("//*[@id='side']/div[1]/div/label/div/div[2]").send_keys(str(entry['PhoneNumber']))
time.sleep(2)
driver.find_element_by_xpath("//*[@id='side']/div[1]/div/label/div/div[2]").send_keys(Keys.ENTER)
time.sleep(2)
driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
#driver.find_element_by_xpath(MESSAGE_INPUT).send_keys(str(entry['Message'])) #AQUII
time.sleep(2)
driver.find_element_by_class_name('_4sWnG').click()
time.sleep(2)
print("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')
Doubts
To make step nr 3. working on python anywhere I have mainly two questions:
- Is it possible to do it without using a headless browser? If yes, do you have any idea how?
- If not, how can I open the 'https://web.whatsapp.com/' URL using a headless browser and making it find the needed elements as I have on the code above? At the moment, to send out a message, I have the code below (error: net::ERR_TUNNEL_CONNECTION_FAILED). The 'https://web.whatsapp.com' is not on the pythonanywhere whitelist so I cannot even test this properly. However, before upgrading my account I would like to be sure that what I want to do could work out somehow (if you are aware of a possible solution that won't need an upgrade, even better).
global driver driver.get('https://web.whatsapp.com/') print("Done updating, check the spreadsheet now") #redirect('https://web.whatsapp.com/', code=302) #comment chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") driver = webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver',options=chrome_options) driver.implicitly_wait(10) waiter = WebDriverWait(driver, 10) for entry in data: driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(str(entry['PhoneNumber'])) time.sleep(2) driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(Keys.ENTER) time.sleep(2) driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message'])) #driver.find_element_by_xpath(MESSAGE_INPUT).send_keys(str(entry['Message'])) #AQUII time.sleep(2) driver.find_element_by_class_name('_4sWnG').click() time.sleep(2) print("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')
I am quite stuck here. Any tip is more than welcome and happy to discuss any possible any other solutions that you guys might find appropriate.