Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to disable open WhatsApp application #24

Closed
madzul opened this issue Sep 6, 2021 · 5 comments
Closed

how to disable open WhatsApp application #24

madzul opened this issue Sep 6, 2021 · 5 comments

Comments

@madzul
Copy link

madzul commented Sep 6, 2021

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

@VanshKela
Copy link
Contributor

Yes can anyone fix this? I guess problem is with wa.me/ link as it is trying to open api.whatsapp.com

@Fahad-Alsaidi
Copy link

I have the same problem

@mlicenblat
Copy link

Hi, I have the same problem.
Sometimes i close the application on every message and it works but i want to know how to do that without this problem with the desktop application.
Any idea? I suppose when you access api.whatsapp.com always tries to use the application

@theCJMan
Copy link
Contributor

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...

@theCJMan
Copy link
Contributor

theCJMan commented 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)

theCJMan added a commit to theCJMan/alright that referenced this issue Apr 21, 2022
Hi there
I have solved Kalebu#24 
In the above code, I left everything the same and created a second send_message calling it send_message1. In this I am not using find user with the suffix link as this causes the desktop WhatsApp to open, I rather browse to a number, and if the number does NOT exist I then handle this problem by testing for the Button and or Message text box. If Button, then the number does not exists...
theCJMan added a commit to theCJMan/alright that referenced this issue Apr 21, 2022
This is to cater for Kalebu#24 making use of send_message1
@Kalebu Kalebu closed this as completed Apr 26, 2022
windragon0910 added a commit to windragon0910/whatsapp_alright_api that referenced this issue Oct 15, 2023
Hi there
I have solved Kalebu/alright#24 
In the above code, I left everything the same and created a second send_message calling it send_message1. In this I am not using find user with the suffix link as this causes the desktop WhatsApp to open, I rather browse to a number, and if the number does NOT exist I then handle this problem by testing for the Button and or Message text box. If Button, then the number does not exists...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants