0

I'm having an input field where we need to enter some values, but then that input field will update with currency value, but when I automate this filled value set to empty once after click on another element. How do I figure this out?

I have attached the screenshot of my application:

screenshot

And below is my code I'm using:

jse.executeScript("document.getElementById('Amount1').setAttribute('value', '20')");

Even I tried using TAB key events, but still the same behaviour is there.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cindy87
  • 47
  • 10
  • Are you using IE for execution? And why are trying to set the value using js, normal `.send_keys` method should do the job, right? – supputuri Apr 16 '20 at 04:19
  • @supputuri No i'm using Chrome. I used .sendkeys and it was not working then i used js but still it's not working. – cindy87 Apr 16 '20 at 23:56

2 Answers2

0

Try triggering the onchange or associated event using the below funciton.

public static void jsTriggerEventOnElement(WebDriver driver, WebElement element, String eventName) { 
        String jsFunction = " var genericEvent = document.createEvent ('Event');  clickEvent.initEvent ('" + eventName + "', true, true); arguments [0].dispatchEvent (genericEvent); ";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(jsFunction, element);
}

You can trigger the event by using the below line of code after entering the value in the field.

// enter the value using the normal send_keys/js

// now call the jsTriggerEventOnElement method, here we are triggering the onchange event 
// please change the event based the associated event
jsTriggerEventOnElement(driver, element, 'onchange`)

Please refer to this post that will give an idea to find out the associated events. But, you can always try with onchange as a first check.

Edit

Try with the below.

public static void jsTriggerEventOnElement(WebDriver driver, WebElement element, String eventName) { 
        String jsFunction = " var clickEvent = document.createEvent ('Event');  clickEvent.initEvent ('" + eventName + "', true, true); arguments [0].dispatchEvent (clickEvent); ";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(jsFunction, element);
    }
jsTriggerEventOnElement(driver,element, 'onblur');
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

Finally i was able to find an answer for this issue. One main reason was the element was stale once after entering the values so i had to re initialise them again.(but i don't think it's the best coding practice)

javascriptHelper.enterTextToField(amount2, driver.findElement(“id of the amount field”));


    javascriptHelper.clickOnElement(WebElementExtension.WaitForElementId(driver, "id of the amount field",
            driverWaitHelper.waitingTime()));

    WebElement textEleAmount2 = driver.findElement(By.id("id of the amount field"));
    textEleAmount2.sendKeys(Keys.TAB);


    javascriptHelper.clickOnElement(WebElementExtension.WaitForElementId(driver, "id of the name field",
            driverWaitHelper.waitingTime()));

    //Here I’m initialising the element again due to stale element reference
    WebElement amountContractor2 = WebElementExtension.WaitForElementId(driver, "id of the amount field",
            driverWaitHelper.waitingTime());

    // erase any existing value in the amount field
    for (int i = 0; i < amountContractor2.getAttribute("data-value").length(); i++) {
        amountContractor2.sendKeys(Keys.BACK_SPACE);
    }

    amountContractor2.sendKeys("300");

    javascriptHelper.jsTriggerEventOnElement(driver, amountContractor2, "change");

    // click away to fire on change event
    javascriptHelper.clickOnElement(driver.findElement(By.id("id of the name field")));
cindy87
  • 47
  • 10