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!