简体   繁体   English

设置输入值而不是 sendKeys() - Selenium WebDriver nodejs

[英]Set value of input instead of sendKeys() - Selenium WebDriver nodejs

I have a long string to test and sendKeys() takes too long.我有一个很长的字符串要测试,而sendKeys()花费的时间太长。 When I tried to set the value of the text the program crashes.当我尝试设置text的值时,程序崩溃了。 I know the Selenium sendKeys() is the best way to test the actual user input, but for my application it takes too much time.我知道 Selenium sendKeys()是测试实际用户输入的最佳方法,但对于我的应用程序来说,它需要太多时间。 So I am trying to avoid it.所以我试图避免它。

Is there a way to set the value right away?有没有办法立即设置值?

See this quick example:请参阅此快速示例:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
      build();

driver.get('http://www.google.com');

// find the search input field on google.com
inputField = driver.findElement(webdriver.By.name('q'));

var longstring = "test"; // not really long for the sake of this quick example

// this works but is slow
inputField.sendKeys(longstring);

// no error but no values set
inputField.value = longstring;

// Output: TypeError: Object [object Object] has no method 'setAttributes'

inputField.setAttributes("value", longstring);

Try to set the element's value using the executeScript method of JavascriptExecutor:尝试使用 JavascriptExecutor 的executeScript方法设置元素的值:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");

Thanks to Andrey Egorov, in my case with python setAttribute not working, but I found I can set the property directly,感谢 Andrey Egorov,在我的情况下,python setAttribute不起作用,但我发现我可以直接设置属性,

Try this code:试试这个代码:

driver.execute_script("document.getElementById('q').value='value here'")

Extending from the correct answer of Andrey-Egorov using .executeScript() to conclude my own question example:使用.executeScript()从 Andrey-Egorov 的正确答案扩展来总结我自己的问题示例:

inputField = driver.findElement(webdriver.By.id('gbqfq'));
driver.executeScript("arguments[0].setAttribute('value', '" + longstring +"')", inputField);

An alternative way of sending a large number of repeating characters to a text field (for instance to test the maximum number of characters the field will allow) is to type a few characters and then repeatedly copy and paste them:将大量重复字符发送到文本字段的另一种方法(例如测试字段允许的最大字符数)是键入几个字符,然后重复复制和粘贴它们:

inputField.sendKeys('0123456789');
for(int i = 0; i < 100; i++) {
    inputField.sendKeys(Key.chord(Key.CONTROL, 'a'));
    inputField.sendKeys(Key.chord(Key.CONTROL, 'c'));
    for(int i = 0; i < 10; i++) {
        inputField.sendKeys(Key.chord(Key.CONTROL, 'v'));
    }
}

Unfortunately pressing CTRL doesn't seem to work for IE unless REQUIRE_WINDOW_FOCUS is enabled (which can cause other issues), but it works fine for Firefox and Chrome.不幸的是,除非启用REQUIRE_WINDOW_FOCUS (这可能会导致其他问题),否则按 CTRL 似乎不适用于 IE,但它适用于 Firefox 和 Chrome。

Thanks to Andrey-Egorov and this answer , I've managed to do it in C#感谢 Andrey-Egorov 和这个答案,我已经成功地用 C# 做到了

IWebDriver driver = new ChromeDriver();
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string value = (string)js.ExecuteScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");

In a nutshell, this is the code which works for me :)简而言之,这是对我有用的代码:)

WebDriver driver;
WebElement element;
String value;

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='"+ value +"';", element);

If you want to use some variable, you may use this way:如果你想使用一些变量,你可以这样使用:

String value= "your value";
driver.execute_script("document.getElementById('q').value=' "+value+" ' ");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('attributeValue').value='new value'");

Thanks guys, here's how I've managed to do this in Java谢谢大家,这是我在 Java 中设法做到这一点的方法

public static void sendKeysJavascript(By element, String keysToSend) {
    WebElement el = driver.findElement(element);
    JavascriptExecutor ex = (JavascriptExecutor) driver;
    ex.executeScript("arguments[0].value='"+ keysToSend +"';", el);
}

有人可以通过两种方式在这里提供帮助它对我不起作用,因为该元素位于 shadow root 下

driver.executeScript("arguments[0].setAttribute('value',arguments[1])",element); driver.executeScript("arguments[0].value='"+ description +"';", element);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM