Having trouble selecting a calendar date using JavaScriptExecutor in Selenium. No errors are popping up in the console, and I can't figure out why. Can someone lend a hand? Check out the Selenium code snippet below.
package SeleniumSessions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SelectCalendarByJS {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","F:\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
WebElement date = driver.findElement(By.xpath("//input[@id = 'departure']"));
String dateVal = "Friday, 19 Jun 2020";
selectDateByJS(driver, date, dateVal);
}
public static void selectDateByJS(WebDriver driver , WebElement element , String dateVal) {
JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("arguments[0].setAttribute('value','"+dateVal+"');", element);
}
}
Here's the HTML DOM for the attribute being selected.
<input data-cy="departure" id="departure" type="text" class="fsw_inputField font20" readonly="" value="Friday, 19 Jun 2020">