The method setAttribute(String, String) is undefined for the type WebElement

2k Views Asked by At

This is my code:

 driver.findElement(By.id("input_17")).setAttribute("value", "selected");

Getting the error

"The method setAttribute(String, String) is undefined for the type WebElement" when I try to change Attribute of webelement.

But for "getAttribute" it is not giving any error.

2

There are 2 best solutions below

0
On

According to documentation WebElement doesn't have setAttribute method. But you could do this using JavascriptExecutor:

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.getElementById('input_17').setAttribute('value', 'selected')");
0
On

you can use .sendkeys instead

driver.findElement(By.id("input_17")).sendKeys("your value");