【问题标题】:Selenium Webdriver - The entered text is vanished when jumping to the next fieldSelenium Webdriver - 跳转到下一个字段时输入的文本消失
【发布时间】:2017-12-01 17:49:13
【问题描述】:

我正在尝试输入文本并确保它被保存。在我的项目中,没有保存按钮,一旦进入它就会被保存。输入文本时只有几个字段让我崩溃。

注意:我使用的是最新版本的 Chrome,并将数值作为文本传递。

我参考了以下页面并相应地更新了我的代码,然后这个问题仍然存在:

下面是我使用的代码,

 public void EnterValuesByIndex(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
      {
        WebElement element = null;

        if (locator.equalsIgnoreCase("cssSelector")) {
          element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);

        } else if (locator.equalsIgnoreCase("xpath")) {
          element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);

        } else if (locator.equalsIgnoreCase("id")) {
          element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1);
        }
        //element.clear();
        element.sendKeys(text);
        Thread.sleep(2000);
        element.sendKeys(Keys.ENTER);
        System.out.println("Enter key is pressed");
      }

以下代码也用于逐字符输入,同样的问题仍然存在,

public void EnterTextbyChar(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
        {
            String value = text;                
            WebElement element = null;

            if (locator.equalsIgnoreCase("cssSelector")) {
            element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);
            } else if (locator.equalsIgnoreCase("xpath")) {
                element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);

            } else if (locator.equalsIgnoreCase("id")){
              element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1);               }
            element.clear();
            for (int i = 0; i < value.length(); i++)
            {
              char c = value.charAt(i);
              String s = new StringBuilder().append(c).toString();
              element.sendKeys(s);
              element.sendKeys(Keys.RETURN);
              element.click();
              Thread.sleep(2000);
              System.out.println("Return key is pressed in EnterTextByChar method");
              System.out.println(c);
             }  
         }

【问题讨论】:

  • 您能具体说明您遇到的实际错误/您面临的问题吗?
  • 没有错误出来。键入的文本在切换到下一个字段后立即消失。因此,必填字段留空,这使我不再继续。

标签: java selenium-webdriver selenium-chromedriver


【解决方案1】:

终于找到了解决办法。输入文本后,发送完美的向上键。

    element.sendKeys(text);
    element.sendKeys(Keys.UP);

【讨论】:

    【解决方案2】:

    如果其他选项不起作用,请尝试 element.sendKeys(文本); element.sendKeys(Keys.RETURN);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多