Python: Selenium write in the text box of a form

Question:

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”.

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?

I tried something like:

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]"}

I appreciate any help on this.

Asked By: TJ1

||

Answers:

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()
Answered By: alecxe