Forums

Open an URL - doubts

Hi!

Use case

Sending out automated WhatsApp messages using pythonanywhere. Step by step logic below:

  1. non-coders write on a gsheet the phone numbers to which we should send out the messages
  2. the gsheet data is read (using gspread in pythonanywhere)
  3. 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:

  1. Is it possible to do it without using a headless browser? If yes, do you have any idea how?
  2. 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.

Ad 1. If you want to use a browser, it needs to be headless, since PythonAnywhere is a headless environment.

Ad 2. You can try the basic Selenium code -- if that works, and your code works on your local machine, you can assume that your code should work, if it's compatible with the headless setup on PA. You can always downgrade and request a refund, within 30 days, if you're not satisfied by the service.

Thanks you so much for your answers! Alright. As WhatsApp asks the QR code to be scanned, I see this as being quite impossible to overcome with a headless browser :( . I will deep dive into it. Meanwhile, if you have any tips for this to work, they are more than welcome.

There are python libraries that could handle QR codes but I do not know what do you need to do with the data you get from it.

Thanks for your reply. The use case of what I am trying to do has to deal with sending automated whatsapp messages. When you try to open the whatsapp on web it should ask you to scan the QR code with your Whatsapp app. This is the step I am trying to bypass but not sure if it would be possible.

That's not something we can help with.