如何设置使用硒输入Web元素的“值”?


问题内容

我的代码中的元素如下所示:

<input id="invoice_supplier_id" name="invoice[supplier_id]" type="hidden" value="">

我想设置它的值,所以我用它的xpath创建了一个web元素:

 val test = driver.findElements(By.xpath("""//*[@id="invoice_supplier_id"]"""))

但是现在我看不到设置值的选项…


问题答案:

使用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")

希望它能对您有所帮助:)