50

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...

Mukesh Takhtani
  • 852
  • 5
  • 15
Joe
  • 2,543
  • 3
  • 25
  • 49
  • 1
    If you're working with an ID, you should use the appropriate By-Locator: `By.id("invoice_supplier_id")` – Peter Wippermann Feb 01 '16 at 09:54
  • 1
    You are currently collecting a list of WebElements. You will need to extract the WebElement from the list, or just find the WebElement by itself. You will also need to unhide the element before Selenium can interact with it. – Ardesco Feb 01 '16 at 10:31

3 Answers3

72

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

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
5
driver.findElement(By.id("invoice_supplier_id")).setAttribute("value", "your value");
Kim Homann
  • 3,042
  • 1
  • 17
  • 20
2

As Shubham Jain stated, this is working to me: driver.findElement(By.id("invoice_supplier_id")).sendKeys("value"‌​, "new value");

eeadev
  • 3,662
  • 8
  • 47
  • 100