Joe
Joe

Reputation: 2633

How to set "value" to input web element using selenium?

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

Upvotes: 56

Views: 219132

Answers (3)

Shubham Jain
Shubham Jain

Reputation: 17553

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

Upvotes: 76

Kim Homann
Kim Homann

Reputation: 3229

driver.findElement(By.id("invoice_supplier_id")).setAttribute("value", "your value");

EDIT: Obviously, the setAttribute() method is not available any more for objects of type WebElement. No idea how to solve this.

Upvotes: 4

eeadev
eeadev

Reputation: 3852

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

Upvotes: 2

Related Questions