Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I am learning Java to automate testcases using WebDriver. As for now I was using Selenium IDE. I have a scenario. In date picker on selecting date, based on the current date and time-future timeslots will be displayed for booking appointments. If current date and time is march 7-2PM, after selecting date, timeslots will apper after 2PM like 2.15,3 etc.. In IDE, I used the locator as //div[4]/ul/li[2]/a. This picks the first item in the list everytime. But when I use the same in WebDriver, only 1 time, it picks the slot. The second time it fails. Any suggestions.

share|improve this question
Make sure your table looks exactly the same as the first time. Maybe the fact that you chose the first slot somehow changed the whole table. –  Eugene S Sep 4 at 2:42

1 Answer 1

I don't understand the 'it works the first time but not the second time' portion. I will say that it is entirely possible that Java is messing up your XPath due to / being an escape character, but I would like to presume you have fixed that.

A way to handle this would be:

List<WebElement> items = driver.findElements(By.cssSelector("ul li"));
if ( items.size() > 0 ) {
  for ( WebElement we: items ) {
   System.out.println( we.getText() );

The XPath method would be a better way to handle it though. What would be changing from the first to the second etc etc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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