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

Pattryk
  • 23
  • 4
  • 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: https://stackoverflow.com/questions/59752886/how-to-set-text-in-textarea-quickly-using-java-and-selenium/59779306#59779306 – pcalkins Apr 27 '20 at 22:02

2 Answers2

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

debugger89
  • 2,108
  • 2
  • 14
  • 16
  • 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 :) – Pattryk Apr 29 '20 at 19:33
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
Pattryk
  • 23
  • 4