2

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 3

1

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

Sign up to request clarification or add additional context in comments.

14 Comments

This is not working in any way and 'text('tesssst');" i think its not the right format at all. Maybe text='tesssst';, but anyway, it is not working.
testarea is not class name but tag name
It stil doesnt work. I can write if i use send_keys using xpath('/html/body/form/div[4]/div/pre/textarea') or css_selector("textarea") , but i still cant find it using jscript. Wtf is going on?!
Did you try the first answer $('textarea').textContent ="something"
I tried it in the format you wrote it. Not sure if i should add something before $
|
0

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"')

7 Comments

It does not work, even if I change value to text or textContent.
What is the class of the element you are trying to type into? You need to replace className with that
I have updated the pictures in the main post. So ace editor its the thing that displays my texarea. The value contains at value attribute the text i want to edit. Inside chrome, using dev tools i am not allowed to manually change value because it says its too big.
I saw the images, replace "className" with "ace_text-input", it should work
I tried and it does not work. This way i cant get it not via send_keys (and send_keys works with xpath or css selector)
|
0

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

Comments

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.