Webdriver写入文本框中的字符串

2024-05-12 19:52:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的程序写一个字符串到一个文本框与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循环中打印字母时,它只打印第一个字母,甚至不写

如果你找不到问题,请让我知道,我会给更多的块从我的代码


Tags: 函数textinboxwhichforwayclass
1条回答
网友
1楼 · 发布于 2024-05-12 19:52:28

您可以使用^{}^{}来填充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

相关问题 更多 >