使用 Selenium 和 python 在 'div' 中插入文本



关于用selenium将文本插入div标记,您还有什么想法吗?下面是我的例子。我想在div标签之间插入一些长字符串

这个例子奏效了,但字符串很长,它会永远持续下去。

text_area.clear()
text_area.send_key(very_long_string)

这个例子不起作用:

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

应该更正此错误,以便从javascript层设置文本字段的"值"。

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)

参考:https://www.w3schools.com/jsref/prop_textarea_value.asp

我发现了如何在web中复制字符串并将其粘贴为文本。下面的示例将字符串值从代码复制到剪贴板中,类似于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

但我在寻找其他东西,因为"echo"不适用于很长的字符串。在我的任务中,我必须将整个.xml文件的值粘贴到文本区域中,我这样做了。

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

最新更新