Skip to content

username_exists function #16

@reydeargentina

Description

@reydeargentina

Hi all, first of all let me say it's my first time interacting like this on Github. I'll try my best.
I found a problem while trying to send a message to more than one destination, when one of those was not saved as a contact. What happened was that the find_by_username function performed the search, but if there was no result for the search, there was no special output to detect it. So, the chat that was open before that search was the one who received the next message. And would continue to get messages until the search gets a result.
I created a new function called username_exists, with find_by_username as the base, which returns true or false whether the search for username opens the username chat or not (by looking at the title of the open conversation).
Here is the code I made. Please, consider I'm just an amateur programmer.

def username_exists(self, username):
        """username_exists ()

        Return True or False whether the contact exists or not, and selects the contact if it exists

        Args:
            username ([type]): [description]
        """
        try:
            search_box = self.wait.until(EC.presence_of_element_located(
                (By.XPATH, '//*[@id="side"]/div[1]/div/label/div/div[2]')))
            search_box.clear()
            search_box.send_keys(username)
            search_box.send_keys(Keys.ENTER)
            m = self.browser.find_element_by_xpath("/html/body/div/div[1]/div[1]/div[4]/div[1]/header/div[2]/div[1]/div/span")
            title = m.get_attribute("title") 
            if title == username:
                return True
            else:
                return False
        except Exception as bug:
            error = f'Exception raised while finding user {username}\n{bug}'
            print(error)

Finally, let me say thanks for alright, and I hope this is useful in some way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions