gasmsk gasmsk - 4 months ago 14
Java Question

How to clear values filled in the input by JS using Selenium WebDriver?

So for the sake of learning, I am trying to automate the search process in Expedia.com
my code selects "bundle deals", then "flight+Hotel+Car" then starts entering values. (depending on your location, you might see different sets of panels)

The problem is, I'm trying to automate the departure and arrival date by selecting them and inserting a date in mm/dd/yyyy format. Everything goes perfectly until it tries to input the date in returning date. It inputs the date, and then JS changes the date back, and my test fails.

public void selectDepartingDate(String date)
{
WebElement dte= driver.findElement(departDate);
dte.sendKeys(date);


}

public void selectReturningDate(String date)
{
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement dte= driver.findElement(arrivalDate);

dte.sendKeys(date);


}


How exactly can i solve this?

Answer

It seems you need to click and clear it before sendKeys:

public void selectReturningDate(String date)
{
    WebElement dte= driver.findElement(arrivalDate);
    det.click();
    dte.clear();
    dte.sendKeys(date);
}