Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (150 points)
closed by anonymous

I'm finding error in "user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))"
Error is: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@title = "Major project"]"}
(Session info: chrome=81.0.4044.92)

Whereas the name of group exists. Please help me. (I found the code in a youtube channel: Get Set Python)

from selenium import webdriver

driver = webdriver.Chrome('/home/rockie/Downloads/chromedriver')
driver.get('https://web.whatsapp.com/')

name = input('Enter the name of user or group : ')
msg = input('Enter your message : ')
count = int(input('Enter the count: '))

input('Enter anything after scanning QR code')

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

msg_box = driver.find_element_by_class_name('_1Plpp')

for i in range(count):
    msg_box.send_keys(msg)
    button = driver.find_element_by_class_name('_35EW6')
    button.click()

closed

3 Answers

0 votes
by (1.3k points)
selected by anonymous
 
Best answer

To correct the provided code, you can make the following modifications:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('/home/rockie/Downloads/chromedriver')

driver.get('https://web.whatsapp.com/')

name = input('Enter the name of user or group: ')

msg = input('Enter your message: ')

count = int(input('Enter the count: '))

input('Enter anything after scanning QR code')

try:

    # Wait until the user element is visible

    user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f'//span[@title="{name}"]')))

    user.click()

    # Wait until the message box element is visible

    msg_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_1Plpp')))

    for i in range(count):

        msg_box.send_keys(msg)

        # Wait until the send button element is visible

        button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_35EW6')))

        button.click()

finally:

    driver.quit()

The modifications include:

  1. Importing the necessary modules (By, WebDriverWait, expected_conditions) from selenium.webdriver.
  2. Adding explicit waits to ensure that the required elements are present before performing actions on them.
  3. Enclosing the code within a try block to handle any exceptions and ensure that the driver is properly quit even if an error occurs.

With these changes, the code should run correctly, allowing you to send messages on WhatsApp using Selenium.

0 votes
by (1.3k points)

To correct the provided code, you can make the following modifications:

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome('/home/rockie/Downloads/chromedriver') driver.get('https://web.whatsapp.com/') name = input('Enter the name of user or group: ') msg = input('Enter your message: ') count = int(input('Enter the count: ')) input('Enter anything after scanning QR code') try: # Wait until the user element is visible user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f'//span[@title="{name}"]'))) user.click() # Wait until the message box element is visible msg_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_1Plpp'))) for i in range(count): msg_box.send_keys(msg) # Wait until the send button element is visible button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_35EW6'))) button.click() finally: driver.quit()

The modifications include:

  1. Importing the necessary modules (By, WebDriverWait, expected_conditions) from selenium.webdriver.
  2. Adding explicit waits to ensure that the required elements are present before performing actions on them.
  3. Enclosing the code within a try block to handle any exceptions and ensure that the driver is properly quit even if an error occurs.

With these changes, the code should run correctly, allowing you to send messages on WhatsApp using Selenium.

0 votes
by (300 points)

Here is the right code for Whatsapp automation using python:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

# Set the path to the Chrome driver executable

driver = webdriver.Chrome('/path/to/chromedriver')

# Open WhatsApp Web

driver.get('https://web.whatsapp.com/')

# Wait for the user to scan the QR code

input('Press Enter after scanning the QR code')

# Locate the user or group

user_name = "John Doe"

user_element = WebDriverWait(driver, 10).until(

    EC.presence_of_element_located((By.XPATH, f'//span[@title="{user_name}"]'))

)

user_element.click()

# Find the message input box

msg_box = WebDriverWait(driver, 10).until(

    EC.presence_of_element_located((By.CLASS_NAME, '_1Plpp'))

)

# Compose and send a message

message = "Hello, this is an automated message!"

msg_box.send_keys(message)

send_button = WebDriverWait(driver, 10).until(

    EC.presence_of_element_located((By.CLASS_NAME, '_35EW6'))

)

send_button.click()

# Quit the driver and close the browser

driver.quit()

In this example, we first import the necessary modules from the Selenium library. We then set the path to the Chrome driver executable and create a new instance of the Chrome WebDriver. The script opens WhatsApp Web by navigating to the URL.

After prompting the user to scan the QR code, the script locates the desired user or group by their name using an XPath expression. It then clicks on the user or group to open the chat.

Next, the script finds the message input box and enters the desired message into it. Finally, it locates and clicks the send button to send the message.

At the end of the script, the driver is properly quit, and the browser is closed.

Please note that you need to replace '/path/to/chromedriver' with the actual path to your Chrome driver executable. Also, make sure you have installed the Selenium library and have the Chrome driver downloaded and available in the specified path.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...