Skip to content
Advertisement

WhatsApp Web Automation with Python Selenium (unable to locate element)

I am using python and selenium to send a message to a target.

I can successfully open whatsapp web but after that I cannot open the inbox of the contact to whom I want to send the message.

Here is the code so far. The first part is common where I have to open the web page. It happened without any problem. The next part is to open the contact to send the inbox message. I tried two different methods for it. Neither of them worked.

Common Part:

#WhatsApp Automation Project
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC 
from time import sleep

#Open WhatsApp web
driver =  webdriver.Chrome('D:/Drivers/chromedriver')
driver.get('https://web.whatsapp.com')


#The code should wait sometime for the user to scan the bar code.
sleep(15)
print('Code ended its pause.')

#chose whom to send messaage.
target = 'Name_in_Contacts'

#choose the message to send.
string = 'Guess who learned to autoate WhatsApp using Python.'

Method 1:

# what I initially thought of doing

search  = driver.find_element_by_class_name("_3FeAD uu8jX")
#class_name is the name of the label of the search box in whatsapp web
#alternatively I had used class name('_3u328 copyable-text selectable-text') , a div class inside the  #label class
#both the class name give the same error.
#check the image of the HTML code.
search.send_keys(target)

Image of HTML code.

It gives error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"._3FeAD uu8jX"}
  (Session info: chrome=80.0.3987.132)

Method 2: This method is copied from one of a solution from Stack OverFlow.

#Stack OverFlow method. Didn't work.

#Open searcch box
search = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME, "_3u328 copyable-text selectable-text")))
#Alternatively, the other class_name of label was also used.
#Both gave the same error.
search.send_keys(target)

It gives error:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

It’s been more than a month now and I cannot solve the problem.

Please do not close the question saying it is already asked. I know there are tons of questions on this topic but neither of them worked for me and it would be great help if you would give me the solution specific to my problem.

Thanking you in advance.

Advertisement

Answer

Finally, I got the answer.

I realize there is no point in opening the search box. Typing name in the search box only brings the name appear on top, it cannot select any of the contacts.

We should find the name of the contact from the center-left side of the screen.

For that use:

user = driver.find_element_by_xpath("//span[@title='{}']".format(name))
user.click()

It opens the inbox.

The link to complete project is here: https://github.com/AshTiwari/WhatsApp-Automation-using-selenium-and-Python Please give a star as it will really encourage me.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement