Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text to a page using python selenium?

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()
like image 644
Chris Avatar asked Dec 06 '25 03:12

Chris


1 Answers

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");
like image 104
alecxe Avatar answered Dec 07 '25 17:12

alecxe