1

I'm trying to access a textbox using selenium in python to execute javascript. I am successfully typing in the message but need to simulate an 'enter' keyboard event to submit the message. I am an absolute beginner in HTML and Javascript but from my understanding the textbox has no input id, only a content-editable text field, which may be causing issues.

Previously, I tried using selenium to access the textbox element via its id, xpath, and class name and using send_keys. This did not work as I got an 'element not reachable by keyboard' error. Now, I am trying to execute javascript directly in the browser (firefox). Sending 'hello world!' works by editing the inner html of the p-tag section, but I cannot press enter on it. So far I have tried using dispatchEvent with the following code, which does not show any errors in the browser console but does not enter the message.

driver.execute_script("document.querySelector('#msg_input > div:nth-child(1) > p:nth-child(1)').innerHTML = '" + "hello world!" + "'") #this line works        

driver.execute_script("""
                      const keyEnterEvent = new KeyboardEvent('keyboard', {
                          bubbles: true, cancelable: true, keyCode: 13
                      });
                      document.querySelector('#msg_input > div:nth-child(1) > p:nth-child(1)').dispatchEvent(keyEnterEvent);
""") #this part does not work

I am hoping to get this or a similar method of simulating the enter key working, any help would be much appreciated!

ejtoast
  • 13
  • 3
  • Perhaps this would help? https://stackoverflow.com/questions/1629053/typing-enter-return-key-in-selenium – Tim Morton Apr 30 '19 at 03:32
  • Thanks @TimMorton - I think all those are suggesting to use the send_keys method which hasn't worked for me because the element is not interactable (I think because it is not an input field, but a content-editable field). Not sure how to get around this! – ejtoast Apr 30 '19 at 03:39
  • 1
    Could you post the html and the error you got when you tried `send_keys`? – S Ahmed Apr 30 '19 at 03:41
  • Instead of using _execute javascript_ and _press enter_ addressing the `element not reachable by keyboard` error would be easier – undetected Selenium Apr 30 '19 at 07:52
  • This might be of some use to you https://a9t9.com/kantu/docs/selenium-ide/sendkeys-type and look for the X-type command. – Tim Morton Apr 30 '19 at 18:25

1 Answers1

0

I just tested the 'contenteditable' element on Mozilla docs (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable), and it appears that it's possible to edit by clicking into the element, typing, and then tabbing out of it (rather than pressing Enter per your example).

If you're not running the automation in a headless browser, I'd simulate this with macro functionality in something like AppRobotic by looking up the X,Y coordinates of the element:

import win32com.client
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver

# navigate to Google
driver = webdriver.Firefox()
driver.get('https://www.google.com') 

# use UI Item Explorer tool to find the X,Y coordinates of Search box
x.MoveCursor(438, 435)
# click inside Search box
x.MouseLeftClick

x.Type("hello world!")
x.Type("{TAB}")
x.Type("{ENTER}")
James
  • 39
  • 2