首页 > 解决方案 > 如何通过 Selenium 和 Python 根据 html 将文本发送到 textarea

问题描述

我想在文本框中单击并发送文本,但找不到该元素。

这是我要单击并发送文本的 html-

<form class="addComment expand" data-id="9656414">
<img 
src="https://ctl.s6img.com/society6/img/g2taHIrokQ01R_67jS8ulaWI2wk/h_150,w_150/users/avatar/~artwork/s6-original-art-uploads/society6/uploads/u/sul97/avatar_asset/d837ee10016843a3bba9ae3310cc338d" width="25" height="25">
                                <textarea placeholder="Add a comment..." data-button="9656414"></textarea>
                                <button id="b9656414">Comment</button>
                            </form>

我的代码:-

driver.find_element_by_class_name('add').click()
comments = driver.find_element_by_xpath("/html/body/form[2]")
comments.click()
comments.send_keys("Awesome Art")

我可以点击但不能在上面输入文字。我究竟做错了什么?

标签: pythonseleniumxpathcss-selectorswebdriver

解决方案


如果要在文本区域输入文本,您需要找到文本区域:

driver.find_element_by_xpath("//textarea[@data-button='9656414']")

推荐阅读