Selenium - send_keys()发送不完整的字符串 [英] Selenium - send_keys() sending incomplete string

查看:1184
本文介绍了Selenium - send_keys()发送不完整的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem: I have a method to fill a field, but the problem is that selenium is not sending the complete string to the field, so my assert fails at the time of verification.

我的代码:

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);

    });
};


推荐答案

看起来这是一个常见问题。 [0]

It looks like this is a common issue. [0]

在尝试解决方法之前,作为完整性检查,确保输入字段已准备好在您发送密钥时接收输入。您也可以在调用SendKeys之前尝试清除该字段。我假设你看到你的字符串被截断,而不是字符缺失或带有一些工件的前缀(如占位符文本或前一个测试的剩余输入)。

Before trying the workarounds, as a sanity check, make sure that the input field is ready to receive input by the time you are sending keys. You could also try clearing the field before calling SendKeys. I am assuming that you are seeing your string truncated, and not characters missing or being prefixed with some artifact (like placeholder text or leftover input from a previous test).

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

Some workarounds if that didn't work:


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

  1. Set the value of the input field using JavaScript, instead of calling SetKeys. On some websites where I do this, the input value actually won't be recognized unless I also trigger an input changed event.

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

Example in C#. Hopefully the only change you need is to make ExecuteScript be executeScript instead.

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

您当然可以将其拆分为两行,一行设置值,第二行设置为派遣活动。


You could of course split this up into two lines, one to set the value, and the second to dispatch the event.

单独发送每个密钥。这是一个解决方法,我已经从线程中看到过几次这个问题。

Send each key individually. This is a workaround I've seen a couple times from the threads about this issue.

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


[0] < a href =https://sqa.stackexchange.com/questions/10450/selenium-sendkeys-not-completing-data-entry-before-moving-to-next-field> https://sqa.stackexchange.com / questions / 10450 / selenium-sendkeys-not-completed-data-entry-moving-to-next-field

https://github.com/angular/protractor/issues/3196

https://github.com/angular/protractor/issues/2019

等。更多如果你想为你的问题寻找其他可能的解决方案,可以通过简单搜索webdriver sendkeys不等待所有密钥找到线程。

[0] https://sqa.stackexchange.com/questions/10450/selenium-sendkeys-not-completing-data-entry-before-moving-to-next-field
https://github.com/angular/protractor/issues/3196
https://github.com/angular/protractor/issues/2019
etc. etc. More threads can be found by a simple search of "webdriver sendkeys does not wait for all the keys" if you want to look for other possible solutions to your issue.

这篇关于Selenium - send_keys()发送不完整的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆