Updated answer to click next date.
//div[contains(@class, 'ui-datepicker-group-first')]//td[@data-month='9' and @data-year='2018']/a[.=31]
You can modify the above XPATH
to select date based on YEAR/MONTH/DATE
. for more XPath creation go-through my answers
.
var path ="//div[contains(@class, 'ui-datepicker-group-first')]//td[@data-month='9' and @data-year='2018']/a[.=31]";
var elem = document.evaluate(path, window.document, null, 9, null ).singleNodeValue;
console.log( elem );
elem.click();
When you enter FROM
and TO
data, then DEPART DATE
field get auto selected. So, just you need to select the first data using javascript.
FROM « //div[@id='ctl00_mainContent_ddl_originStation1_CTNR']//a[@text='Guwahati (GAU)']
TO « //div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[@text='Goa (GOI)']
DEPART DATE «
//div[contains(@class, 'ui-datepicker-group-first')]//a[contains(@class, 'ui-state-active')]
sample test program.
import io.github.yash777.driver.Browser;
import io.github.yash777.driver.Drivers;
import io.github.yash777.driver.WebDriverException;
public class SpiceJET {
static WebDriver driver;
static WebDriverWait explicitWait;
public static void main(String[] args) throws WebDriverException, IOException {
test();
}
public static void test() throws WebDriverException, IOException {
Drivers drivers = new Drivers();
String driverPath = drivers.getDriverPath(Browser.CHROME, 63, "");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver( capabilities );
explicitWait = new WebDriverWait(driver, 10);
//Maximize browser window
driver.manage().window().maximize();
//Go to URL which you want to navigate
driver.get("https://spicejet.com/");
clickElement("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT'][1]");
clickElement("//div[@id='ctl00_mainContent_ddl_originStation1_CTNR']//a[@text='Guwahati (GAU)']");
clickElement("//input[@id='ctl00_mainContent_ddl_destinationStation1_CTXT'][1]");
clickElement("//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[@text='Goa (GOI)']");
clickUsingJavaScript("//div[contains(@class, 'ui-datepicker-group-first')]//a[contains(@class, 'ui-state-active')]");
}
}
public static void clickElement(String locator) {
By findBy = By.xpath( locator );
WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));
element.click();
}
public static void clickUsingJavaScript( String locator ) {
StringBuffer click = new StringBuffer();
click.append("var elem = document.evaluate(\""+locator+"\", window.document, null, 9, null ).singleNodeValue;");
click.append("elem.click();");
System.out.println("JavaScript Click.");
jse.executeScript( click.toString() );
}
For Automatic management of Selenium Driver Executable’s in run-time for Java use SeleniumWebDrivers
NOTE: If you are selecting DEPART DATE
which got auto selected then selenium throws exception
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error:
Element <input type="text" readonly="readonly" id="ctl00_mainContent_view_date2" class="custom_date_pic required home-date-pick">
is not clickable at point (784, 241). Other element would receive the click: <span class="ui-datepicker-month">...</span>