简体   繁体   中英

Unable to change the date using Javascript in Selenium WebDriver

I am unbale to change the date which is visible in the Web Page using Javascript code as below:

HTML Source Code:

  /** * <input id="Datefromdate" * class="ui-input-text ui-body-b ui-corner-all ui-shadow-inset" type="text" * placeholder="Click here to select From Date" id="Datefromdate" * name="Datefromdate" required="required" * onchange="setMinEndDate(this.id);" ele_type="start" * class="ui-input-text ui-body-b ui-corner-all ui-shadow-inset" * readonly=""> */ 

Javascript used to change Date:

JavascriptExecutor js = (JavascriptExecutor) getDriverInstanceForThread();
        WebElement element = getDriverInstanceForThread().findElement(By.id("Datefromdate"));
        js.executeScript("arguments[0].setAttribute('value', '10/16/2014')",element);

Date field displays current date, if I try to get the value using "value" attribute as shown below, but unable to change its value using "value" attribute using js as shown above.

driver.findElement(By.id("Datefromdate")).getAttribute("value");

output = 09/16/2014

Please help to find the solution..

In your code setAttribute() is not valid Javascript.

You need to change this line:

js.executeScript("arguments[0].setAttribute('value', '10/16/2014')",element);

to this:

js.executeScript("arguments[0].value='10/16/2014'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM