0

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.

  • What was the error? What happened vs what did you expect to happen? – Dominik Mar 03 '21 at 06:39
  • It's likely going to be an input field which means `innerText` is the wrong API here. You will likely need `value` – Dominik Mar 03 '21 at 06:42
  • Try `document.getElementsByClassName('_2_1wd copyable-text selectable-text')[1].value = "hello this is test"` and use the `submit` event on the form to submit. – Dominik Mar 03 '21 at 06:44
  • 1
    Might need to notify the page, [example](https://stackoverflow.com/a/64096646). – wOxxOm Mar 03 '21 at 06:46
  • @Dominik, I tried by using 'value' but it doesn't work, because element is 'div' in its nature not an input field. – Taurus Codes Mar 03 '21 at 06:51
  • @TaurusCodes we won't be able to help you if we don't know the html of that page. Try to create a re-producible, self-contained example of your problem here on SO. Preferably use the code snippet function – Dominik Mar 03 '21 at 10:01

1 Answers1

-3

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"

Karthikeyan
  • 406
  • 3
  • 14