question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to disable open WhatsApp application

See original GitHub issue

everytime i run the code (from the examples), a web browser (Chrome) opened and then ask me to open whatsapp application (https://api.whatsapp.com wants to open this application) if I canceled, then the browser do nothing, otherwise the whatsapp application is opened and alright gave me error: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
theCJMancommented, Apr 21, 2022

Hi all, Solved it

The problem is that self.suffix_link = “https://wa.me/” is causing the desktop app to open, or rather causing the browser to ask if it should open the desktop app.

If you call web.open(f"https://web.whatsapp.com/send?phone={phone_no}&text={quote(message)}") this does NOT cause the browser to ask if it should open the desktop app

So I generated an additional send_message1 in init.py code file, I have tried to add many comments in the code so it should be clear

   def send_message1(self, mobile: str, message: str):
        """ CJM - 20220419:
            Send WhatsApp Message With Different URL, NOT using https://wa.me/ to prevent WhatsApp Desktop to open
            Also include the Number we want to send to """
        try:
            # Browse to a "Blank" message state
            self.browser.get(f"https://web.whatsapp.com/send?phone={mobile}&text")

            # This is the XPath of the message textbox
            inp_xpath = (
                '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]'
            )
            # This is the XPath of the "ok button" if the number was not found
            nr_not_found_xpath = (
                '//*[@id="app"]/div/span[2]/div/span/div/div/div/div/div/div[2]/div/div'
            )

            # If the number is NOT a WhatsApp number then there will be an OK Button, not the Message Textbox
            # Test for both situations -> find_elements returns a List
            ctrl_element = self.wait.until(
                lambda ctrl_self:
                    ctrl_self.find_elements(By.XPATH, nr_not_found_xpath) or
                    ctrl_self.find_elements(By.XPATH, inp_xpath)
            )
            # Iterate through the list of elements to test each if they are a textBox or a Button
            for i in ctrl_element:
                if i.aria_role == 'textbox':
                    # This is a WhatsApp Number -> Send Message
                    i.send_keys(message + Keys.ENTER)
                    msg = f"Message sent successfully to {self.mobile}"

                elif i.aria_role == 'button':
                    # This is NOT a WhatsApp Number -> Press enter and continue
                    i.send_keys(Keys.ENTER)
                    msg = f"Not a WhatsApp Number {self.mobile}"

        except (NoSuchElementException, Exception) as bug:
            print(bug)
            msg = f"Failed to send a message to {self.mobile}"

        finally:
            print(msg)

And then my Example code is reading a CSV file and sending WhatsApp messages to them one by one, In South Africa (+27) our mobile numbers is only 10 digits starting with a 0 e.g. 0790445672

from alright import WhatsApp
import pandas as pd

msg = "Kiss"

messenger = WhatsApp()

df = pd.read_csv("DataFile\contacts.csv")
#Conver the data to String
df['Mobile Phone'] = df['Mobile Phone'].astype(str)
df['Mobile Phone'] = df['Mobile Phone'].str.replace(' ', '')
for i, row in df.iterrows():
    mobNum = row['Mobile Phone']
    if len(mobNum) < 10:
        #Do nothing
        print("Number to short " + mobNum)
    elif (len(mobNum) == 10) and (mobNum[0] == '0'):
        #Add Country Code
        mobNum = '27' + mobNum[1:]
        messenger.send_message1(mobNum, msg)
    elif len(mobNum) >= 10:
        #Assume Country Code already in number
        messenger.send_message1(mobNum, msg)
0reactions
theCJMancommented, Apr 13, 2022

I have solved this problem. The reason for this is the use of the self.suffix_link = "https://wa.me/" in init.py file with the use of the messenger.find_user('<NUMBER>') From what I can see this ind_user only obtains a URL from WhatsApp that then is used. However, If you “Build” this URL your self then you do not need this function

In the same Function same file, I (for now) hard coded the url self.BASE_URL = "https://web.whatsapp.com/send?phone=<NUMBER>&text" The rest I left the same, BUT I then only need to call this:

from alright import WhatsApp
messenger = WhatsApp()
messenger.send_message("Kiss, the Test Hallo ")

I will work more on to do this dynamically…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Disable WhatsApp Without Deleting The App - Cashify
Step 1: Open the phone Settings and go to the apps. · Step 2: From various applications, find and select WhatsApp. · Step...
Read more >
3 Ways to Disappear From WhatsApp Without Deleting The App
3. Force Stop WhatsApp · Navigate your phone settings. · Go to Apps. · Select WhatsApp and tap on Force Stop.
Read more >
How to Disable WhatsApp Automatic Startup - HardReset.info
First, open Settings. Arrow pointing on the Settings App on device. Then, scroll down and find Manage apps. ... Next, find and open...
Read more >
How to Enable or Disable WhatsApp from Opening at Startup
First, launch the WhatsApp application on your computer. Click the ellipsis icon (three horizontal dots) to the top left of the screen and ......
Read more >
How to disappear from WhatsApp without deleting the app ...
1. Disable WhatsApp tune: 2. Disabling notifications in the form of WhatsApp icons or dots: 3. Disable notification light:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found