-1

Website : https://www.abhibus.com/

I am trying to automate selecting date of journey on the website above. Nothing I have tried is working.

Screenshot of the date selector https://i.stack.imgur.com/2RWbG.jpg

Code I am using

 WebDriverWait wait2=new WebDriverWait(driver,5);
 WebElement startdate = wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='ui-datepicker-calendar']//td[@data-month='9']//a[.='22']"))); 
     startdate.click();

Error message:

no such element: Unable to locate element: {"method":"xpath","selector":"//table[@class='ui-datepicker-calendar']//td[@data-month='9']//a[.='22']"}
2
  • Welcome to the site. When you say it's not working, what does that mean? Are you getting any error messages? If so, please add that to your question.
    – Lee Jensen
    Oct 21, 2021 at 15:24
  • Thank you..... this is the message i am getting i tryed in many other xpaths also but every time i am getting this error .................. : no such element: Unable to locate element: {"method":"xpath","selector":"//table[@class='ui-datepicker-calendar']//td[@data-month='9']//a[.='22']"} Oct 21, 2021 at 15:40

2 Answers 2

0

Try like below and confirm:

driver.get("https://www.abhibus.com/");
WebDriverWait wait = new WebDriverWait(driver,30);
        
// Click on the "Date of journey"
wait.until(ExpectedConditions.elementToBeClickable(By.id("datepicker1"))).click();
        
// try to click on the date since past dates will be disabled.
try {
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ui-datepicker-div']/div[1]//a[text()='27' and not(contains(@class,'disabled'))]"))).click();
    System.out.println("Selected date");
}catch (TimeoutException e){
    System.out.println("Invalid date");
}
1
  • 2
    Why should this work? What mistakes the OP made? It's not only about code snippets, but also about little bit of explanation here.
    – pavelsaman
    Oct 24, 2021 at 16:14
0

You made the mistake of writing you automation without taking your domain into consideration. Let look at your selector

//table[@class='ui-datepicker-calendar']//td[@data-month='9']//a[.='22'] 

The first part is absolutely fine, you go straight and root yourself in the interesting sub component. On the second value, you define that september (data-month=9) will always be available, which it isn't since your application under test doesn't give data attributes to disabled fields but marks them just as

ui-datepicker-unselectable
If you just exchange the "9" in your selector for a 10, it will work again.

Of course you might not want to have an automation that needs to be fixed every month, since the change of a month is a quite predictable event so if you want to select the 22nd of the current month you might wanna try something along the lines of

LocalDate currentdate = LocalDate.now();
String selector = "//table[@class='ui-datepicker-calendar']//td[@data-month='"+currentdate.getMonth()+"']//a[.='22']"
 WebDriverWait wait2=new WebDriverWait(driver,5);
 WebElement startdate = wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(selector))); 
 startdate.click();

which would insert the current month into your selector. If that isn't your intended behaviour, you might want to actually define a function to construct your selector according to your specification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.