Home > front end >  WhatsApp text field only showing first character of alphabet when send_keys method is used
WhatsApp text field only showing first character of alphabet when send_keys method is used

Time:08-05

I have identified the following html code as the input box: I know whatsapp changes this from thime to time but this one for now works.

<div role="textbox" spellcheck="true" title="Type a message" data-testid="conversation-compose-box-input" data-tab="10" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true" contenteditable="true"><p ><br></p></div>

My selenium python code is

driver.get('https://web.whatsapp.com/')
user_name = 
user = driver.find_element(By.XPATH, f'//span[@title="{user_name}"]')
user.click()
mes = driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]')
mes.click()
mes.clear()
mes.send_keys('Again this is a trial')```

whenever i execute this code.

only "A" appears in textbox.

i have tried various div containers but only this one seems to at least give affirmation that this is the input box.
what are some ways in which i can send whole message using the send_keys method.


CodePudding user response:

In order to input the entire text you must split your string into characters and send character by character into the inputfield using a foor loop.

for (int k = 0; i < your_string.length; k  ) {
    your_field.sendKeys(your_string.charAt(k));
}

CodePudding user response:

you can use js to write message as

js_send_message = """
// wait until paste all text
function waitForPastedData(elem, old) {
  if (elem.childNodes && elem.childNodes.length == old) {
    return true
  }
  else {
      old = elem.childNodes.length
      return new Promise((resolve, reject) => {
     setTimeout(() => {
       resolve(waitForPastedData(elem, old));
     }, 2000);
   });
  }
}
async function send_text(text) {
    const dataTransfer = new DataTransfer();
    dataTransfer.setData('text', text);
    const event = new ClipboardEvent('paste', {
          clipboardData: dataTransfer,
          bubbles: true
        });
    let el = document.querySelector('#main .copyable-area [contenteditable="true"][role="textbox"]')
    el.focus()
    document.execCommand("selectall");
    el.dispatchEvent(event)
    return await waitForPastedData(el, 0)
}
return await send_text(arguments[0])
"""
res = driver.execute_script(js_send_message, text)

now you can send long message with new line (\n) and emoji

  • Related