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:
val test = driver.findElements(By.xpath("""//*[@id="invoice_supplier_id"]"""))
but now I dont see an option to set the value...
We can set any attribute value of a webelement in Selenium. Selenium can run Javascript commands by the executeScript method. The command to be executed is passed as an argument to the method.
The getAttribute() method helps to get the value of any attribute of a web element, which is returned as a String. If an attribute has a Boolean value, the method returns either True or null. Also, if there is no attribute, the method will return null.
We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method. Let us consider a textbox where we entered some text and then want to get the entered text.
Use findElement
instead of 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 using 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);
OR (in javascript)
driver.findElement(By.xpath("//input[@id='invoice_supplier_id'])).setAttribute("value", "your value")
Hope it will help you :)
driver.findElement(By.id("invoice_supplier_id")).setAttribute("value", "your value");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With