Webdriver-在文本框中写入字符串

时间:2018-12-24 21:55:43

标签: python selenium-webdriver

我正在尝试让我的程序将字符串写入带有for循环的文本框中,但我似乎做不到。

我试图在有功能和无功能的情况下进行操作,但我对其进行了调试,但仍然找不到问题

HTML:

<textarea class="challengeTextArea" rows="4"></textarea>

我尝试过的方法(python):

challange = "Dear, dear! How queer it seems, Alice said to herself, Which way? Which way?, holding her hand in hand with Dinah, and saying Come up again, dear! I shall"
text_box = driver.find_element_by_xpath("//textarea[@class='challengeTextArea']")
for letter in challange:
    print(letter)
    text_box.send_keys(letter)
print("IM HERE")

我希望它能写到文本文件中,但是当我在for循环中打印字母时,它只打印第一个字母,甚至不写它。.

如果找不到问题,请告诉我,我将从代码中提供更多内容。

1 个答案:

答案 0 :(得分:0)

您可以使用send_keys()send_keys_to_element()来填充textarea,即:

from selenium import webdriver
driver = webdriver.Chrome()
html_content = """
<html>
     <head></head>
     <body>
        <textarea class="challengeTextArea" rows="4"></textarea>
     </body>
</html>
"""
driver.get("data:text/html;charset=utf-8," + html_content)
challenge = "Dear, dear! How queer it seems, Alice said to herself, Which way? Which way?, holding her hand in hand with Dinah, and saying Come up again, dear! I shall"
text_box = driver.find_element_by_xpath("//textarea[@class='challengeTextArea']")
text_box.send_keys(challenge)

输出:

enter image description here