【问题标题】:Selenium WebDriver: I want to overwrite value in field instead of appending to it with sendKeys using JavaSelenium WebDriver:我想覆盖字段中的值,而不是使用 Java 使用 sendKeys 附加到它
【发布时间】:2010-07-14 19:10:07
【问题描述】:

在 WebDriver 中,如果我使用 sendKeys,它会将我的字符串附加到字段中已经存在的值。我无法使用 clear() 方法清除它,因为第二次这样做,网页会抛出一个错误,说它必须在 10 到 100 之间。所以我无法清除它,否则之前会抛出错误我可以使用 sendKeys 输入新值,如果我 sendKeys 它只是将它附加到已经存在的值。

WebDriver 中是否有任何内容可以让您覆盖字段中的值?

【问题讨论】:

    标签: java selenium-webdriver sendkeys


    【解决方案1】:

    您也可以在发送密钥之前清除该字段。

    element.clear()
    element.sendKeys("Some text here")
    

    【讨论】:

    • True_Blue 在问题中提到他不能使用 clear() 因为第二次他这样做,网页会抛出一个错误,说它必须在 10 到 100 之间。
    • 好电话。我会在这里保留我的答案,以防将来对某人有用,因为标题没有具体说明。
    • 如果此解决方案对某人不起作用,请提供其他信息:在调用 element.clear() 之前确保 element.GetAttribute("value") 确实有一个值(等待此值不为空)。有时会在使用 ngModel 指令测试 AngularJS 输入时发生。
    • 为我工作,即使是空字段。
    【解决方案2】:

    我认为你可以尝试先选择字段中的所有文本,然后发送新序列:

    from selenium.webdriver.common.keys import Keys
    element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
    

    【讨论】:

    • 是的,成功了,我只需要导入 Keys 类。非常感谢。
    • 我正在 Pyton 中尝试。如何导入Keys
    • @BaoNgo 对于 Mac,您需要使用 Keys.COMMAND 而不是 Keys.CONTROL
    • @SergiiPozharov 是否有与此命令等效的 Python?我使用了send_keys,但出现错误AttributeError: type object 'Keys' has no attribute 'chord'
    • 找到了。 element.send_keys(Keys.CONTROL, 'a')
    【解决方案3】:

    好吧,那是几天前... 在我目前的情况下,ZloiAdun 的答案对我不起作用,但让我非常接近我的解决方案......

    代替:

    element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
    

    下面的代码让我很开心:

    element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55");
    

    所以我希望对某人有所帮助!

    【讨论】:

    • 我用过这个-> element.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT, Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN));这太神奇了,它在 textarea 框中选择了 3 行文本,因为命令 A 对我也不起作用
    【解决方案4】:

    这对我有用。

    mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE);
    

    【讨论】:

    • 这样应该会更好,Ctrl+A 可能无法在西班牙语键盘/Windows 中使用。
    【解决方案5】:

    如果它对任何人有帮助,ZloiAdun 的 C# 等效答案是:

    element.SendKeys(Keys.Control + "a");
    element.SendKeys("55");
    

    【讨论】:

      【解决方案6】:

      使用这个,它是值得信赖的解决方案,适用于所有浏览器:

      protected void clearInput(WebElement webElement) {
          // isIE() - just checks is it IE or not - use your own implementation
          if (isIE() && "file".equals(webElement.getAttribute("type"))) {
              // workaround
              // if IE and input's type is file - do not try to clear it.
              // If you send:
              // - empty string - it will find file by empty path
              // - backspace char - it will process like a non-visible char
              // In both cases it will throw a bug.
              // 
              // Just replace it with new value when it is need to.
          } else {
              // if you have no StringUtils in project, check value still empty yet
              while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
                  // "\u0008" - is backspace char
                  webElement.sendKeys("\u0008");
              }
          }
      }
      

      如果输入有 type="file" - 不要为 IE 清除它。它会尝试通过空路径查找文件并抛出错误。

      更多详情您可以找到on my blog

      【讨论】:

      • 对不起,我理解错了。我的解决方案 - 只是关于清除输入值的正确方法,而不是关于如何用新的一键操作替换当前值
      【解决方案7】:

      由于文本字段不接受键盘输入,使用上述大多数方法时遇到问题,并且鼠标解决方案似乎不完整。

      这用于模拟字段中的点击,选择内容并将其替换为新内容。

           Actions actionList = new Actions(driver);
           actionList.clickAndHold(WebElement).sendKeys(newTextFieldString).
           release().build().perform();
      

      【讨论】:

        【解决方案8】:

        使用以下内容:

        driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value");
        

        【讨论】:

          【解决方案9】:

          这很容易做到,而且对我有用:

          //Create a Javascript executor
          
          JavascriptExecutor jst= (JavascriptExecutor) driver;
          jst.executeScript("arguments[1].value = arguments[0]; ", 55, driver.findElement(By.id("id")));
          

          55 = 赋值

          【讨论】:

            【解决方案10】:

            原始问题说 clear() 不能使用。这不适用于那种情况。我在这里添加我的工作示例,因为这篇 SO 帖子是在输入值之前清除输入的第一个 Google 结果之一。

            对于没有额外限制的输入,我使用 NodeJS 为 Selenium 提供了一种与浏览器无关的方法。这个 sn-p 是我在测试脚本中使用var test = require( 'common' ); 导入的公共库的一部分。它用于标准节点 module.exports 定义。

            when_id_exists_type : function( id, value ) {
            driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 )
                .then( function() {
                    var el = driver.findElement( webdriver.By.id( id ) );
                    el.click();
                    el.clear();
                    el.sendKeys( value );
                });
            },
            

            找到元素,单击它,清除它,然后发送密钥。

            This page has a complete code sample and article 这可能会有所帮助。

            【讨论】:

              【解决方案11】:

              这解决了我在处理带有嵌入 JavaScript 的 HTML 页面时遇到的问题

              WebElement empSalary =  driver.findElement(By.xpath(PayComponentAmount));
              Actions mouse2 = new Actions(driver);
              mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform();
              
              JavascriptExecutor js = (JavascriptExecutor) driver;
              js.executeScript("arguments[0].onchange()", empSalary);
              

              【讨论】:

                【解决方案12】:
                 WebElement p= driver.findElement(By.id("your id name"));
                 p.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
                

                【讨论】:

                • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2017-07-06
                • 2013-02-09
                • 2015-03-21
                • 1970-01-01
                • 2014-10-24
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多