[英]Javascript to input value into textbox instead of sendKeys() in Python
[英]Set value of input instead of sendKeys() - Selenium WebDriver nodejs
我有一個很長的字符串要測試,而sendKeys()
花費的時間太長。 當我嘗試設置text
的值時,程序崩潰了。 我知道 Selenium sendKeys()
是測試實際用戶輸入的最佳方法,但對於我的應用程序來說,它需要太多時間。 所以我試圖避免它。
有沒有辦法立即設置值?
請參閱此快速示例:
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);
嘗試使用 JavascriptExecutor 的executeScript
方法設置元素的值:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");
感謝 Andrey Egorov,在我的情況下,python setAttribute
不起作用,但我發現我可以直接設置屬性,
試試這個代碼:
driver.execute_script("document.getElementById('q').value='value here'")
使用.executeScript()
從 Andrey-Egorov 的正確答案擴展來總結我自己的問題示例:
inputField = driver.findElement(webdriver.By.id('gbqfq'));
driver.executeScript("arguments[0].setAttribute('value', '" + longstring +"')", inputField);
將大量重復字符發送到文本字段的另一種方法(例如測試字段允許的最大字符數)是鍵入幾個字符,然后重復復制和粘貼它們:
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'));
}
}
不幸的是,除非啟用REQUIRE_WINDOW_FOCUS
(這可能會導致其他問題),否則按 CTRL 似乎不適用於 IE,但它適用於 Firefox 和 Chrome。
感謝 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')");
簡而言之,這是對我有用的代碼:)
WebDriver driver;
WebElement element;
String value;
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='"+ value +"';", element);
如果你想使用一些變量,你可以這樣使用:
String value= "your value";
driver.execute_script("document.getElementById('q').value=' "+value+" ' ");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('attributeValue').value='new value'");
謝謝大家,這是我在 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.