How to use Javascriptexecutor for entering text in an element which does not have Id as a locator?

14,504

Try the following code, it would solve your problem:

 WebElement element = driver.findElement(By.xpath("enter the xpath here"));
 JavascriptExecutor jse = (JavascriptExecutor)driver;
 jse.executeScript("arguments[0].value='enter the value here';", element);
Share:
14,504
Kovid Mehta
Author by

Kovid Mehta

Updated on June 25, 2022

Comments

  • Kovid Mehta
    Kovid Mehta almost 2 years

    I want to enter text in an Textbox which does not take the input through SendKeys. I proceeded with using Javascriptexecutor to enter the text and succeeded. Now there are a few fields in which there is no Id that can be selected as a locator so I need to locate them with Xpath. I would like to know how can we locate the element by xpath in Javascriptexecutor and pass the values to it.

    JavascriptExecutor jse = (JavascriptExecutor)driver
    jse.executeScript("document.getElementById('value').value='1611 
    Dragons';");
    

    I need an approach where I can use it something like:

    document.getElementByXpath("Xpath Here").value='xyz';");
    

    Please feel free to guide me to a Doc/answer if it documented somewhere before.