Clear textarea with facebook webdriver

412 Views Asked by At

I'm using the PHP facebook/webdriver package to handle some web automation testing.

I have been unable to clear the content of a <textarea> element and have run out of options.

Given:

  • a dom element <textarea>content</textarea>
  • $element to be a Facebook\WebDriver\WebDriverElement instance

I have tried calling RemoteWebElement::clear():

// phpdoc for this method states: 
// "If [is] TEXTAREA or text INPUT element, this will clear the value"

var_dump($element->getTagName()); // string(8) "textarea"
var_dump($element->getText());    // string(7) "content"

$element->clear(); 

var_dump($element->getText());    // string(7) "content"

I have tried sending backspace keys:

var_dump($element->getTagName()); // string(8) "textarea"
var_dump($element->getText());    // string(7) "content"

for ($i = 0; $i < mb_strlen($element->getText()); $i++) {
    $element->sendKeys(WebDriverKeys::BACKSPACE);
}

var_dump($element->getText());    // string(7) "content"    

I'm at a bit of a loss. Any ideas?

1

There are 1 best solutions below

0
On

To query the content of a textarea, use $element->getAttribute('value') instead of $element->getText().

RemoteWebElement::getText() appears only to get the text node that was present at the time the page loaded. At least that is my best guess based on observed behaviour.

RemoteWebElement::getAttribute('value') gets the current content of a texarea. I did not guess at this being the correct solution due to a texarea element not having a value attribute in HTML.