3

I am trying to enter some text into a textarea with selenium.

Inspection of the source reveals the following about the textareas code.. my text should be entered where it says "Text goes here":

<iframe id="tinymcewindow_ifr" frameborder="2" src="javascript:""" allowtransparency="true" title="Rich Text AreaPress ALT-F12 for toolbar. Press ALT-0 for help" style="width: 90%; height: 90px; display: block;">

    #document
        <!DOCTYPE >
        <html>
            <head xmlns="http://www.site.org/xhtml"> … </head>
            <body id="tinymce" class="mceContentBody " contenteditable="true" onload="window.parent.tinyMCE.get('tinymcewindow').onLoad.dispatch();" spellcheck="false" dir="ltr">
                <p> … </p>
            </body>
        </html>

</iframe>

When I try and select the textarea with some command like:

driver.find_element_by_name("tinymce")

it returns an error message like this;

 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 164, in execute
   ...

the command:

driver.find_element_by_id("tinymce")

Gives this error message:

 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 197, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 164, in execute
 ...

How does one select a textbox like this with Selenium?

2
  • isn't you content inside an iframe? Commented Dec 1, 2013 at 22:36
  • @javo, yes it's. I have updated the question with the iframe section.. sry everyone, didnt think that was of importance Commented Dec 1, 2013 at 22:41

1 Answer 1

4

try

driver.find_element_by_id("tinymce")

Elements in HTML Forms have name attribute, that is what you tried to match with find_element_by_name.

If you have a form like

<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>

You could match the input for first name with find_element_by_name('firstname'), but that's not what you want to do right now.

The element you want to select looks like this

<body id="tinymce" class="mceContentBody " contenteditable="true" onload="window.parent.tinyMCE.get('tinymcewindow').onLoad.dispatch();" spellcheck="false" dir="ltr">

So you can

  1. find_element_by_id("tinymce")
  2. find_element_by_css_selector("#tinymce")
  3. find_element_by_xpath("//*[@id='tinymce']")
  4. ...

... and for the sake of completeness ...

Once you have selected this element, you can fill it with

driver.find_element_by_id("tinymce").send_keys("Hello world")

EDIT:

Sometimes people don't realise that selecting an element works within the context of current page. If your content is inside an iframe, you need to switch to this iframe first.

driver.switch_to_frame("frameName")

Then you can go back with driver.switch_to_default_content()

See Selenium Python - Navigating

Sign up to request clarification or add additional context in comments.

1 Comment

@user3053161 I've updated my answer, I think you might have problems with iframes - and please think about a selector you use for switch_to_frame, don't just update your question with another stacktrace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.