【问题标题】:Selenium - send_keys() sending incomplete stringSelenium - send_keys() 发送不完整的字符串
【发布时间】:2017-04-20 12:36:49
【问题描述】:

我的问题:我有填充字段的方法,但问题是 selenium 没有将完整的字符串发送到字段,所以我的断言在验证时失败。 p>

我的代码:

var webdriver = require('selenium-webdriver');
var casual = require('casual');
var expect = require('chai').expect;
var By = webdriver.By;

exports.addPropuesta = function (driver) {

var first_name = casual.first_name;

driver.findElement(By.xpath("//a[contains(text(),'Añadir Propuesta Test')]")).click();

name_field = driver.findElement(By.name('nombre'));
name_field.sendKeys(first_name);

driver.findElement(By.css("Input[type='submit']")).click();

driver.findElement(By.css('.table')).getText().then(function(table_content){

    expect(table_content).to.include(first_name);

    });
};

【问题讨论】:

    标签: javascript node.js selenium selenium-webdriver


    【解决方案1】:

    看起来this 是一个常见问题。

    在尝试变通方法之前,作为健全性检查,请确保在您发送密钥时输入字段已准备好接收输入。您也可以在调用 SendKeys 之前尝试清除该字段。我假设您看到您的字符串被截断,而不是缺少字符或以某些工件为前缀(例如占位符文本或先前测试的剩余输入)。

    如果不起作用,一些解决方法:

    1. 使用 JavaScript 设置输入字段的值,而不是调用 SetKeys。在我执行此操作的某些网站上,除非我还触发输入更改事件,否则输入值实际上不会被识别。

      C# 中的示例。希望您需要的唯一更改是将 ExecuteScript 改为 executeScript。

      driver.ExecuteScript("var exampleInput = document.getElementById('exampleInput'); exampleInput.value = '" + testInputValue + "'; exampleInput.dispatchEvent(new Event('change'));");
      

      当然,您可以将其分成两行,第一行用于设置值,第二行用于调度事件。

    2. 单独发送每个密钥。这是我从有关此问题的线程中多次看到的解决方法。

      for (var i = 0; i < first_name.length; i++) {
          name_field.sendKeys(first_name.charAt(i));
      }
      

    https://github.com/angular/protractor/issues/3196
    https://github.com/angular/protractor/issues/2019
    等等等等。如果您想寻找其他可能的解决方案,可以通过简单搜索“webdriver sendkeys 不等待所有密钥”找到更多线程。

    【讨论】:

    • 我尝试使用 executeScript 但出现同样的问题。
    • 第二个选项正常,但是打字明显太慢了……
    【解决方案2】:

    我在以前的版本中遇到过这个问题并提交了错误报告。它已经被修复了,但也许它又被打破了?无论如何,当我们在protractor chat channel 上讨论这个问题时,提出了以下建议:正常使用 sendKeys,然后验证结果。如果结果未通过完整性检查,则一次输入一个字符。

    /**
     * A Typescript version that can be used as a mixin.
     * Make some minor modifications to use as a class.
     * @param data {string} The string to enter in the input element
     */
    export class SendKeys {
        inputEl: ElementFinder;
    
        sendKeys(data: string) {
            var el = this.inputEl;
            // click on the input before sending data. This helps the focus and action situations.
            el.click();
    
            el.clear();
            el.sendKeys(data);
    
            // Verify whether or not hte whole data value was sent.
            // If not, send data one character at a time, which works.
            // See: https://github.com/angular/protractor/issues/3196
            el.getAttribute('value').then(function (insertedValue) {
                if (insertedValue !== data) {
                    // Failed, must send characters one at a time
                    el.clear();
                    for (let i=0; i < data.lenght; i++) {
                        el.sendKeys(data.charAt(i));
                   }
               }
           });
        }
    }
    

    --

    /**
     * The Javascript version:
     * @param el {ElementFinder} The input element reference
     * @param data {string} The string to enter in the input element
     */
    export function sendKeys(el, data) {
            var el = this.inputEl;
            // click on the input before sending data. This helps the focus and action situations.
            el.click();
    
            el.clear();
            el.sendKeys(data);
    
            // Verify whether or not hte whole data value was sent.
            // If not, send data one character at a time, which works.
            // See: https://github.com/angular/protractor/issues/3196
            el.getAttribute('value').then(function (insertedValue) {
                if (insertedValue !== data) {
                    // Failed, must send characters one at a time
                    el.clear();
                    for (let i=0; i < data.lenght; i++) {
                        el.sendKeys(data.charAt(i));
                   }
               }
           });
        }
    

    【讨论】:

      【解决方案3】:

      我也遇到了这个问题,这个解决方法帮助了我。

      1. 将整个文本(需要发送的)复制到剪贴板(使用python“剪贴板”库)。

      2. 激活网页上的字段并发送_keys(Keys.CONTROL + 'v')

         import clipboard
         my_text = "text_to_be_sent"
         clipboard.copy(my_text)
         fld = brw.find_element_by_xpath("//*[contains(text(), 'blah blah blah')]")
         fld.click()
         sleep(4)
         fld.send_keys(Keys.CONTROL + 'v')
        

      【讨论】:

        【解决方案4】:

        我对这个问题的解决方法是在每个 send_keys 之前添加 driver.sleep(1)

        示例:

        driver.sleep(1000);
        driver.findElement(By.name('rut')).sendKeys(rut_text);
        
        driver.findElement(By.name('dv')).sendKeys(dv);
        
        driver.sleep(1000);
        driver.findElement(By.name('nombre')).sendKeys(first_name);
        
        driver.sleep(1000);
        driver.findElement(By.name('apellido_paterno')).sendKeys(apellido_paterno_field);
        
        driver.sleep(1000);
        driver.findElement(By.name('apellido_materno')).sendKeys(apellido_materno);
        
        driver.sleep(1000);
        driver.findElement(By.name('celular')).sendKeys(phone_number);
        
        driver.sleep(1000);
        driver.findElement(By.name('email')).sendKeys(email);
        

        我尝试解决添加execute_scriptclear 但对我来说没有解决。

        【讨论】:

          猜你喜欢
          • 2018-03-28
          • 2020-05-16
          • 1970-01-01
          • 2014-09-24
          • 2015-12-08
          • 2018-09-06
          • 2020-09-02
          • 2016-05-29
          • 2015-01-22
          相关资源
          最近更新 更多