3

I'm trying to enter text into a text box using python selenium.

The html looks like below:

<div class="sendBox placeholder" contenteditable="true" data-placeholder="Type a message or drop attachment..." id="sendMessage" style="height: 375px;"></div>

After manually typing 'TEST' into the text box, the html looks like this:

<div class="sendBox placeholder" contenteditable="true" data-placeholder="Type a message or drop attachment..." id="sendMessage" style="height: 375px;">TEST</div>

I've tried the following code, but there is no response

driver.find_element_by_xpath("//div[@id='sendMessage']").send_keys("testing")

However, if I manually click on the text box so that the cursor shows up and then enter in that code, it does work. I haven't been able to figure out how to make the curser show up via python selenium. I've tried the below though.

driver.find_element_by_xpath("//div[@id='sendMessage']").click()
3
  • What would happen if you first click the element and then send keys to it? Any errors? Thanks. Commented Oct 10, 2015 at 5:45
  • right, so if I do the following two lines successively, there is no error and nothing happens: driver.find_element_by_xpath("//div[@id='sendMessage']").click() driver.find_element_by_xpath("//div[@id='sendMessage']").send_keys("testing") Commented Oct 10, 2015 at 5:47
  • Thanks. Could you also look through the markup and see if there is an input or textarea which is, probably, hidden, but has the same value/innerHTML as your content editable div? Commented Oct 10, 2015 at 5:48

1 Answer 1

3

It sounds like clicking and then sending keys to the element should help:

element = driver.find_element_by_xpath("//div[@id='sendMessage']")
element.click()
element.send_keys("testing")

You can also workaround it with setting the innerHTML via execute_script():

driver.execute_script("arguments[0].innerHTML = arguments[1];", element, "testing");
Sign up to request clarification or add additional context in comments.

2 Comments

like I said, the first option you provided does not work. However, your second option is working kind of. For some reason, the text that shows up in the text box is 'arguments[1]'
@Chris oops, check it out now, please :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.