0

I'm trying to automate messaging in whatsapp web and on my messages, use some cool emojis, but as you know, chromedriver by default to not type emoji characters with send_keys().

So for testing, my code is based on this answer here, which it is working fine, but if I try to send a multiple unicode emoji, this error shows up:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing command parameters

Sample of my current code:

chatBox = self.driver.find_element_by_class_name('_3uMse')
time.sleep(2)
chatBox.click()     
        
JS_ADD_TEXT_TO_INPUT = """
var elm = arguments[0], txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new Event('change'));
"""
text = u'\uD83D\uDC6A'
#using this emoji, it works  
#text = u'\u263A'

self.driver.execute_script(JS_ADD_TEXT_TO_INPUT, chatBox, text)
chatBox.send_keys(text)

Why is this happening?


Edit 1 - full code with suggested changes to help debbuging

self.driver.get('https://web.whatsapp.com/')
# time to scan qrcode
time.sleep(8)
contact = self.driver.find_element_by_xpath(f"//span[@title='<NAME_OF_SAVED_CONTACT>']")
contact.click()

chatBox = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3uMse")))
chatBox.click()     

JS_ADD_TEXT_TO_INPUT = """
var elm = arguments[0], txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new Event('change'));
"""
text = u'\uD83D\uDC6A'
self.driver.execute_script(JS_ADD_TEXT_TO_INPUT, chatBox, text)
chatBox.send_keys(text)
João Vitor
  • 267
  • 1
  • 5
  • 23

1 Answers1

-2

This error message...

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing command parameters

...implies that the arguments passed to a command are either invalid or malformed.


Invalid argument errors are majorly linked to TypeErrors in JavaScript and they can occur when the input value is not of the expected type or malformed in some way. Possibly in this usecase the issue was the chatBox wasn't interactable when you tried to send the emoji through execute_script().


Solution

A probable solution would be to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • CodeBlock:

    chatBox = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3uMse")))
    chatBox.click()     
    
    JS_ADD_TEXT_TO_INPUT = """
    var elm = arguments[0], txt = arguments[1];
    elm.value += txt;
    elm.dispatchEvent(new Event('change'));
    """
    text = u'\uD83D\uDC6A'
    self.driver.execute_script(JS_ADD_TEXT_TO_INPUT, chatBox, text)
    chatBox.send_keys(text)
    
  • 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
    

However, this code block works just perfect at my end to send complex emoticons (Emoji) with multiple unicodes e.g. family using u\uD83D\uDC6A as follows:

  • Code Block:

    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://www.google.com/')
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
    JS_ADD_TEXT_TO_INPUT = """
      var elm = arguments[0], txt = arguments[1];
      elm.value += txt;
      elm.dispatchEvent(new Event('change'));
      """
    text = u'\uD83D\uDC6A'
    driver.execute_script(JS_ADD_TEXT_TO_INPUT, element, text)
    
  • Browser Snapshot:

emoji_chrome


Note

Finally, it's worth to mention that https://web.whatsapp.com/ elements are ReactJS enabled elements. So classes like _3uMse are dynamically generated and are never static. You may require to construct the locators based on static attributes.

You can find a relevant discussion in How to click on a username within web.whatsapp.com using Selenium and Python

Additionally as per the discussion in Element Send Keys to file input with 'multiple' attribute should append file list you need to ensure that your ChromeDriver version is at least of ChromeDriver-75.


References

You can find a couple of relevant discussions in:


Outro

A couple of relevant discussions in ChromeDriver forum:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Thanks for this awesome answer @DebanjanB, but unfortunately the error persists, I guess it's something with the WhatsApp web which in trying to automate on, because the example with google worked perfectly! I just edited my question and added the full code to help – João Vitor Jun 23 '20 at 19:27
  • @JoãoVitor Checkout the answer update. Possibly the referenced discussion [How to click on a username within web.whatsapp.com using Selenium and Python](https://stackoverflow.com/questions/61898797/how-to-click-on-a-username-within-web-whatsapp-com-using-selenium-and-python/61901500#61901500) will solve your issue. – DebanjanB Jun 23 '20 at 19:41