0

I want to create a program, that can read all the messages from my whatsApp and print them to the screen using python.

In order to do that I tried using the whatsapp-web library https://pypi.org/project/whatsapp-web/.

But when i tried to run their code example I got a timeout error

this is the code

import time

from selenium import webdriver

from simon.accounts.pages import LoginPage
from simon.header.pages import HeaderPage
from simon.pages import BasePage

# Creating the driver (browser)
driver = webdriver.Firefox()
driver.maximize_window()

login_page = LoginPage(driver)
login_page.load()
login_page.remember_me = False

time.sleep(7)

base_page = BasePage(driver)

base_page.is_welcome_page_available()
base_page.is_nav_bar_page_available()
base_page.is_search_page_available()
base_page.is_pane_page_available()
base_page.is_chat_page_available()

# 3. Logout
header_page = HeaderPage(driver)
header_page.logout()

# Close the browser
driver.quit()


and this is the error

    base_page.is_welcome_page_available()
  File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 18, in wrapper
    return func(*args, **kwargs)
  File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 51, in is_welcome_page_available
    if self._find_element(WelcomeLocators.WELCOME):
  File "D:\zoom\venv\lib\site-packages\simon\pages.py", line 77, in _find_element
    lambda driver: self.driver.find_element(*locator))
  File "D:\zoom\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
yardenK
  • 183
  • 1
  • 3
  • 14
  • I have similar issue, error message like following: Traceback (most recent call last): File "getmsg.py", line 31, in opened_chats = pane_page.opened_chats File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\simon\chats\elements.py", line 31, in __get__ lambda driver: driver.find_elements(*self.locator)) File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 87, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Messag – Sal Jun 17 '21 at 19:48

1 Answers1

0
  • Your code is not completed to find error or correct.

  • Because the error is coming from the imported page.

  • Try by increasing the time limit to load the page, use time.sleep(15)

  • You can try to automate the WhatsApp web by yourself without using pip of WhatsApp-web. because I guess that is not updated.

  • The code is giving error because of WhatsApp web has changed its elements classes names. Because of that the code section is not able to find welcome page in given time limit. So the program execution breaks

  • I have done the same without using WhatsApp-web pip.

  • I hope it will work for you.

A complete code reference Example :


from selenium import webdriver
import time

# You can use any web-browser which supported by selenium and which can run WhatsApp web.
# For using GoogleChrome
web_driver = webdriver.Chrome("Chrome_Driver_Path/chromedriver.exe")
web_driver.get("https://web.whatsapp.com/")

# For using Firefox
# web_driver = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
# web_driver.get("https://web.whatsapp.com/")


time.sleep(25) # For scan the qr code
# Plese make sure that you have done the qr code scan successful.
confirm = int(input("Press 1 to proceed if sucessfully login or press 0 for retry : "))
if confirm == 1:
   print("Continuing...")
elif confirm == 0:
   web_driver.close()
   exit()
else:
   print("Sorry Please Try again")
   web_driver.close()
   exit()
while True:
    unread_chats = web_driver.find_elements_by_xpath("// span[@class='_38M1B']")
    # In the above line Change the xpath's class name from the current time class name by inspecting span element
    # which containing the number of unread message showing the contact card inside a green circle before opening the chat room.
    
    # Open each chat using loop and read message.
    for chat in unread_chats:
        chat.click()
        time.sleep(2)
        # For getting message to perform action
        message = web_driver.find_elements_by_xpath("//span[@class='_3-8er selectable-text copyable-text']")
        # In the above line Change the xpath's class name from the current time class name by inspecting span element
        # which containing received text message of any chat room.
        for i in message:
            try:
                print("Message received : " + str(i.text))
                # Here you can use you code to perform action according to your need
             except:
                pass

  • Please make sure that the indentation is equal in code blocks if you are copying it.

  • Can following link for more info about WhatsApp web using python.

  • https://stackoverflow.com/a/68288416/15284163

  • I am developing WhatsApp bot using python.

  • For contribution you can contact at : anurag.cse016@gmail.com

  • Please give a star on my https://github.com/4NUR46 If this Answer helps you.

  • Consider that the question is about a timeout error, using the Firefox driver, and this solution uses Chrome.exe, which may not be available on the user's machine. There also appear to be some typos in the web address, does this code work? – Mike Fiedler Jul 30 '21 at 22:26
  • I checked, but the error is not related to the web browser driver. User can use Firefox in place of Chrome. There is no error in the code because user have copied the example of that(whatsapp-web) library to use. The main error is coming from the function written inside that whatsapp-web library which is get imported by the user. I have updated info about the reason of occurrence of that timeout error which is coming during the running the program. And yeah I made many typo there in my program, Thanks for informing and correcting me. Please comment if have any other query. – Anurag Kushwaha Jul 31 '21 at 23:21