How to locate and write text into textarea using python and javascript?

Viewed 551

Hei,

I am trying to write something inside a textarea element with python selenium and a javascript: The js is: self.driver.execute_script("document.getElementsByClassName('textarea').value='tesssst';")

But it does not get the text there and I have tried with other get elements by (..).

It works if I use self.driver.find_element_by_css_selector("textarea").send_keys(code)

or by xpath. But the problem with this method is that 'code' variable is pretty big and javascript works faster. Also, if I use

send_keys(keys.control + c/v)

it gives me an extra dot at the start of the text and I have no idea why would that even happen. Any sugestions?

Ace Editor

value

HTML Code

More Html

3 Answers

self.driver.execute_script( "$('pre#editor').text("jsdasdsjj");")

Or

self.driver.execute_script( "arguments[0].text('tesssst');",self.driver.find_element_by_css_selector("pre#editor"))

enter image description here

use textContent to set the value

getElementsByClassName returns an array, so I think you need to get the first element of that array. Try the following code:

driver.execute_script('document.getElementsByClassName("className")[0].value="yourValue"')

I have been trying out the options provided in PDHide's answer, and the following worked for me finally:

self.driver.execute_script( "arguments[0].value += 'tesssst';",self.driver.find_element_by_css_selector("pre#editor"))

For your reference, I am using selenium version 3.1.4, chrome version: 91.0.4472.114 and ChromeDriver 91.0.4472.19

Related