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