Search code examples
javascriptweb-scrapinggoogle-chrome-extensionselenium-chromedriverwebautomation

How to fill the web whatsapp text field in javascript?


I am trying to fill the text field in Web WhatsApp using Javascript but it is not working according to my requirement.

I want to write the text in the text field and then click the Send button programmatically using JS.

Is that possible?

I tried following code in console,

document.getElementsByClassName('_2_1wd copyable-text selectable-text')[1].innerText = "hello this is test\\n"

ScreenShot enter image description here

It is writing in the text field but overlapping the existing placeholder and send button also doesn't show by trying programmatically.


Solution

  • It is not advisable,

    But you can do it by triggering events..

    var inputEvent = new InputEvent('input', {
        bubbles: true,
        cancelable: true,
    });
    var clickEvent = new InputEvent('click', {
        bubbles: true,
        cancelable: true,
    });
    
    var selectableTextDiv = select that textDiv;
    selectableTextDiv.innerText = "You desired Message";
    selectableTextDiv.dispatchEvent(inputEvent)
    // After triggeing input event.. you will see the send button
    var sendButton = //selectThatButton
    sendButton .dispatchEvent(clickEvent);
    

    But as I mentioned above "It is not advisable"