binoua binoua - 7 months ago 10
Python Question

Selenium: check textarea is cleared on click

I'm using selenium with Python.
I'm writing a functional test in which I want to check that when a user clicks on a textarea, it clears its content.

This is the HTML code for the text area:

<textarea id="id_textarea" cols="50" rows="10" onclick="this.value=''">Enter some text</textarea>


It works in my browser but I can't get it working with selenium and my test fails over and over again.
This is my Python code:

# User clicks on the text area which removes its content.
input_textarea.click()
self.assertEqual(input_textarea.text, '')


Note that my point is not to use the selenium
input_textarea.clear()
function as I precisely want to check that the HTML code handles this part.

Any help would be appreciated.

Ben

Answer

You should use get_attribute('value') method instead of text:

input_textarea.click()
self.assertEqual(input_textarea.get_attribute('value'), '')

P.S. I assume that input_textarea is something like driver.find_element_by_id('id_textarea')