java - 如何设置 "value"以使用 selenium 输入 web 元素?

标签 java selenium xpath selenium-webdriver selenium-chromedriver

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

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

希望对你有帮助:)

关于java - 如何设置 "value"以使用 selenium 输入 web 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127108/

相关文章:

java - 构建当前的 Selenium ?

php - DOMDocument/Xpath 在长时间的命令行过程中泄漏内存 - 任何解构此类的方法

java - JTextArea 不显示在 JTabbedPane 内的 JPanel 上

java - 通过 CLI 执行 testng.xml 时出错 : Could not find or load main class org. testng.TestNG

java - 在单元测试中检查工厂的结果

java - 忽略 headless Chrome 中的证书错误

java - Selenium - 尝试使用 "contains"通过其内容来定位表格单元格

java - 如何使用java在文档中追加新节点

java - 如何获取 paint/paintComponent 生成的图像?

java - 可以使用 Retrofit 进行 SOAP web 服务调用吗?