我的代码中有如下所示的元素:
I have element in my code that looks like this:
<input id="invoice_supplier_id" name="invoice[supplier_id]" type="hidden" value="">
我想设置它的值,所以我用它的 xpath 创建了一个 web 元素:
I want to set its value, so I created a web element with it's xpath:
val test = driver.findElements(By.xpath("""//*[@id="invoice_supplier_id"]"""))
但现在我没有看到设置值的选项...
but now I dont see an option to set the value...
使用 findElement
代替 findElements
driver.findElement(By.xpath("//input[@id='invoice_supplier_id'])).sendKeys("your value");
或
driver.findElement(By.id("invoice_supplier_id")).sendKeys("value", "your value");
或使用 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);
或
(JavascriptExecutor) driver.executeScript("document.evaluate(xpathExpresion, document, null, 9, null).singleNodeValue.innerHTML="+ DesiredText);
或(在 javascript 中)
driver.findElement(By.xpath("//input[@id='invoice_supplier_id'])).setAttribute("value", "your value")
希望对你有帮助:)
这篇关于如何设置“价值"?使用硒输入网页元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!