¿cómo eliminar los valores predeterminados en el campo de texto usando selenium?


Quiero eliminar un valor predeterminado de un cuadro de texto para ingresar el nuevo valor, pero no entiendo cómo hacerlo.

Yo estaba pensando en usar CTRL+un y luego Delete, pero no estoy seguro de cómo hacer esto.

Incluso usé el comando de WebDriver driver.findElement("locator").clear();.

Author: Petr Janeček, 2012-05-29

8 answers

Si está buscando una solución de Selenium RC, puede usar simplemente

// assuming 'selenium' is a healthy Selenium instance
selenium.type("someLocator", "");
 2
Author: Petr Janeček,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-29 15:25:11

¿Y fue útil el código? Porque el código que estás escribiendo debe hacer la cosa:

driver.findElement("locator").clear();

Si no ayuda, entonces intente esto:

WebElement toClear = driver.findElement("locator");
toClear.sendKeys(Keys.CONTROL + "a");
toClear.sendKeys(Keys.DELETE);

Tal vez usted tendrá que hacer alguna conversión de la Keys.CONTROL + "a" a consecuencia de caracteres, pero el primer enfoque debe hacer la magia

 38
Author: Pavel Janicek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-29 13:18:53

Para el modelo de objeto de página -

 @FindBy(xpath="//foo")
   public WebElement textBox;

Ahora en su función

 public void clearExistingText(String newText){
    textBox.clear();
    textBox.sendKeys(newText);
  }

Para la arquitectura general de selenio -

driver.findElement(By.xpath("//yourxpath")).clear();
driver.findElement(By.xpath("//yourxpath")).sendKeys("newText");
 3
Author: Shek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-18 14:24:39

Puede utilizar el siguiente código. Selecciona el valor preexistente en el campo y lo sobrescribe con el nuevo valor.

driver.findElement(By.xpath("*enter your xpath here*")).sendKeys(Keys.chord(Keys.CONTROL, "a"),*enter the new value here*);
 2
Author: Eric Fernandes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-23 13:54:07

La siguiente función eliminará el carácter de entrada uno por uno hasta que el campo de entrada esté vacío usando PromiseWhile

driver.clearKeys = function(element, value){
  return element.getAttribute('value').then(function(val) {
    if (val.length > 0) {
      return new Promise(function(resolve, reject) {
        var len;
        len = val.length;
        return promiseWhile(function() { 
          return 0 < len;
        }, function() {
          return new Promise(function(resolve, reject) {
            len--;
            return element.sendKeys(webdriver.Key.BACK_SPACE).then(function()              {
              return resolve(true);
            });
          });
        }).then(function() {
          return resolve(true);
        });
      });
    }
 1
Author: uri wald,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-27 04:03:25

Esto ha funcionado para mí:

driver.findElement(yourElement).clear();

driver.findElement(yourelement).sendKeys("");

 1
Author: kljaksa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-26 21:37:53

.clear() se puede usar para borrar el texto

  (locator).clear();

Usando clear con el localizador borra todo el valor en ese localizador exacto.

 0
Author: Sanju Abel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-06 09:20:06

driver.findElement(locator).clear() - Este comando funcionará en todos los casos

 0
Author: dom thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-15 10:30:59