Java 如何使用硒设置“值”以输入网络元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35127108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set "value" to input web element using selenium?
提问by Joe
I have element in my code that looks like this:
我的代码中有如下所示的元素:
<input id="invoice_supplier_id" name="invoice[supplier_id]" type="hidden" value="">
I want to set its value, so I created a web element with it's xpath:
我想设置它的值,所以我用它的 xpath 创建了一个 web 元素:
val test = driver.findElements(By.xpath("""//*[@id="invoice_supplier_id"]"""))
but now I dont see an option to set the value...
但现在我没有看到设置值的选项......
采纳答案by Shubham Jain
Use findElement
instead of findElements
使用findElement
代替findElements
driver.findElement(By.xpath("//input[@id='invoice_supplier_id'])).sendKeys("your value");
OR
或者
driver.findElement(By.id("invoice_supplier_id")).sendKeys("value", "your value");
OR (in javascript)
或(在 JavaScript 中)
driver.findElement(By.xpath("//input[@id='invoice_supplier_id'])).setAttribute("value", "your value")
OR using JavascriptExecutor
或使用 JavascriptExecutor
WebElement element = driver.findElement(By.xpath("enter the xpath here")); // you can use any locator
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='enter the value here';", element);
OR
或者
(JavascriptExecutor) driver.executeScript("document.evaluate(xpathExpresion, document, null, 9, null).singleNodeValue.innerHTML="+ DesiredText);
Hope it will help you :)
希望它会帮助你:)
回答by Kim Homann
driver.findElement(By.id("invoice_supplier_id")).setAttribute("value", "your value");
回答by eeadev
As Shubham Jain stated, this is working to me: driver.findElement(By.id("invoice_supplier_id")).sendKeys("value"??, "new value");
正如 Shubham Jain 所说,这对我有用: driver.findElement(By.id("invoice_supplier_id")).sendKeys("value"??, "new value");