Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, selecting a textbox with selenium

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?

like image 665
user3053161 Avatar asked Feb 13 '26 01:02

user3053161


1 Answers

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

like image 139
Jan Vorcak Avatar answered Feb 15 '26 15:02

Jan Vorcak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!