0

Do you have any more ideas about inserting text into div tag with selenium? Below are my examples. I want to insert some long string between div tags

THIS EXAMPLE WORKED, but with very long string it lasts forever.

text_area.clear()
text_area.send_key(very_long_string)

THIS EXAMPLE DOES NOT WORK:

text_area = driver.find_element_by_xpath("//div[@id='divtextarea1']").text
#example above ^ doesnt work without .text/.value either
driver.execute_script("arguments[0] = arguments[1]", text_area, my_text)
'''

1
  • you'll want to send keys to the input tag, not the div. This will "type" the characters. You can also paste them in if it takes too long. See this answer: stackoverflow.com/questions/59752886/… Commented Apr 27, 2020 at 22:02

2 Answers 2

0

This should be corrected to set the "value" of the text fields from the javascript layer.

text_area = driver.find_element_by_xpath("//div[@id='divtextarea1']")
# Need to make sure the "value" attribute is set from the JS command
driver.execute_script("arguments[0].value = arguments[1]", text_area, my_text)

reference: https://www.w3schools.com/jsref/prop_textarea_value.asp

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

1 Comment

idk why, but with div "arguments[0].value" return null. I can see div value with "arguments[0].text", but execute_script does not work. I found out an solution for me, ill leave it in comment below. Thanks for your advices :)
0

I found out how to copy string and paste it as text in web. Example below copy string value from code into clipboard, like with ctrl+c

string_variable = "test"
os.system('echo %s| clip' % string_variable) #copy string, like ctrl+c
text_area.send_keys(Keys.CONTROL + "v")      #paste string, like ctrl+v

But I was looking for something else, because 'echo' does not work with very long string. In my task i had to paste whole .xml file value into text area and i did it like below.

xmlfile = "C:\\Users\\myfile.xml"
os.system('type "%s" | clip' % xmlfile)      #copy whole file value(string), like ctrl+c
text_area.send_keys(Keys.CONTROL + "v")      #paste string, like ctrl+v

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.