如何设置“值”以使用 selenium 输入 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"]"""))

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

原文由 Joe 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 451
1 个回答

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

希望它会帮助你:)

原文由 Shubham Jain 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题