0

I'm trying to send automatic messages on WhatsApp Web (MacOS, Firefox), I want to use Selenium (pywhatkit also works), but send_keys sends only the first one character.

All the "import" of the code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.service import Service
import time

TXT_BAR='/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div/[2]/div/div[2]/div[1]'

txt_bar = wait.until(EC.presence_of_element_located((By.XPATH, TXT_BAR)))

txt_bar.click()
txt_bar.clear()

txt_bar.send_keys("help")
time.sleep(5)
txt_bar.send_keys(Keys.ENTER)
time.sleep(2)
#output -> h

edited code:

txt_bar = wait.until(EC.element_to_be_clickable((By.XPATH, TXT_BAR)))
txt_bar.click()
txt_bar.clear()
txt_bar.send_keys("help")
txt_bar.send_keys(Keys.ENTER)
#output -> h
Thias
  • 1
  • 2

2 Answers2

1

I have facing a similar problem like this. May be this is a security measure used facebook to prevent automation. one particular solution is to use a loop. Here is a example: Texts="hello, world" For text in texts: send_keys(text)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Akzy Mar 22 '23 at 05:21
0

WhatsApp web elements are dynamic elements. Ideally, to send a character sequence to the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

txt_bar = wait.until(EC.element_to_be_clickable((By.XPATH, TXT_BAR)))
txt_bar.click()
txt_bar.clear()
txt_bar.send_keys("help")
txt_bar.send_keys(Keys.ENTER)

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • What error do you see? Remember we still don't know anything about `TXT_BAR` – undetected Selenium Aug 05 '22 at 23:21
  • Right, it makes sense, but when I correct my code, the problem remains. I’m sorry but I’m new to formatting comments on Stackoverflow, there’s no "error" I think, `TXT_BAR = '/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div/[2]/div/div[2]/div[1]' `should be right, I can see the character. – Thias Aug 05 '22 at 23:24
  • `txt_bar = wait.until(EC.element_to_be_clickable((By.XPATH, TXT_BAR))) txt_bar.click() txt_bar.clear() txt_bar.send_keys("help") txt_bar.send_keys(Keys.ENTER)` The output it's still "h". – Thias Aug 05 '22 at 23:36
  • I have already added these import. – Thias Aug 05 '22 at 23:43