我要测试的字符串很长,而且sendKeys()时间太长。当我尝试设置程序的值时text崩溃。我知道Selenium sendKeys()是测试实际用户输入的最佳方法,但是对于我的应用程序来说,这会花费太多时间。因此,我试图避免这种情况。
sendKeys()
text
有没有一种立即设置值的方法?
请看以下快速示例:
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);
尝试使用executeScript方法设置元素的值:
executeScript
webdriver.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");