selenium - 而不是 sendkeys 最后从剪贴板复制的项目被输入到元素中

标签 selenium automation cucumber selenium-chromedriver

在我的应用程序中,我有一个电子邮件字段,我正在使用 sendkeys 发送电子邮件地址,但不是 sendkeys selenium 插入剪贴板中的值。有人遇到过这个问题吗?

代码:

WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("sample@sample.com")

因为 Id 是动态的,所以我从父容器中获取了 xpath

最佳答案

在 Node.js javascript 中,在匈牙利语键盘上,sendKeys 中的 @ 符号已替换为剪贴板的内容。如果您尝试将“@”宽度替换为其 unicode 值“\u0040”,结果是相同的,sendKeys 已替换为剪贴板的内容,即使尝试使用棘手的 Key.chord( Key.ALT, "v", Key.NULL ) 形式,它什么都不做。

// if clipboard contains XXX 
WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("sample@sample.com")
// result: field content is sampleXXXsample.com

如果你在剪贴板上加上@符号,效果很好,但你不能保证测试自动化中剪贴板的内容。有一种方法可以将 key 发送到事件项。

我会避免用javascript填充字段,如上所述,因为javascript直接填充它,不关心字段状态,即使字段不活动也用值填充它,不触发事件等,javascript是蛮力填充字段。

发送。 key 。有一个 npm 包。它是 powershell 发送 key 的包装器,因此仅适用于 Windows。

npm install -g sendkeys

和测试代码:

const sendkeys = require( 'sendkeys');
WebElement email=driver.findElement(By.xpath("//div[@class='auth-container']//input[@type='text']");
email.sendKeys("sample")
// result: cursor is in the field, field contains 'sample'
sendkeys.sync( "@sample.com" )
// result: field content is 'sample@sample.com'

关于selenium - 而不是 sendkeys 最后从剪贴板复制的项目被输入到元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317143/

相关文章:

cucumber - 在 Cucumber Gherkin 的同一场景中运行两个用户故事

gradle - Gradle 项目的 Runner 类中无法解析导入 cucumber

java - 如何从源代码触发 Eclipse PMD 插件功能?

automation - cypress - 执行操作直到元素显示在屏幕上

json - 为什么 Minium Developer 不读取 JSON 文件?

android - 集成测试和 Cucumber 测试

java - TestNG,Selenium如何在多个类之间共享对象

Python Selenium : Dropdown on a popup not interactable

java - TestNG 测试使用一个测试的参数而不是自己的参数

c# - Selenium 2 WebDriver 未按预期评估更新的 DOM