Python:Selenium写在表单的文本框中 [英] Python: Selenium write in the text box of a form

查看:60
本文介绍了Python:Selenium写在表单的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 此处 中的文本框中输入内容.该框的右侧显示在此处粘贴您的文字".

I am trying to write in the text box in here. It is the box that on its right hand it says "Paste your text here".

我想我的问题是如何找到我应该在Selenium驱动程序中向其发送文本的盒子中的物品(例如,按ID)?

I guess my question is how to find the item, for example by id, of the box that I should send text there in selenium driver?

我尝试过类似的事情:

item = driver.find_element_by_css_selector("form#text_processor input[name=process_this]")
item.send_key("Test!")

但是,当我这样做时,我会收到以下错误消息:

But when I do that I get this error message:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"form#text_processor input[name=process_this]"}

我对此表示感谢.

推荐答案

文本区域位于iframe内-切换到该区域,找到该元素并向其发送键:

The text area is inside an iframe - switch to it, find the element and send keys to it:

driver.switch_to.frame("textarea_iframe")
driver.find_element_by_id("textarea_body").send_keys("test")


请注意,要删除文本区域中的现有文本,只需预先选择所有内容:


Note that to delete the existing text in the text area, just pre-select it all:

text_area = driver.find_element_by_id("textarea_body")
text_area.send_keys(Keys.CONTROL, "a")  # or Keys.COMMAND on Mac
text_area.send_keys("test")


此外,如果您需要返回到主要内容,请使用:


Additionally, if you would need to go back to the main content, use:

driver.switch_to.default_content()

这篇关于Python:Selenium写在表单的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆