Java- code to find the earliest date + Selenium WebDriver to perform actions


 Here is an interesting code that will
  1. Pick elements from a webpage 
  2. Convert the text into time               
  3. Put all the collected time into a sorted date set
  4. Pick the earliest date in the set
  5. Use this date to perform desired action on the webpage 
SortedSet dates = new TreeSet(); String time; List elements = driver.findElements(By.xpath("//a[contains(text(), ':')]")); for (WebElement webElement : elements) { time = webElement.getText(); if (!(time.equals(""))) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); Date date = simpleDateFormat.parse(time); //System.out.println(simpleDateFormat.format(date)); dates.add(date); } }
Date earliest = dates.first(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); String found = sdf.format(earliest); String id = driver.findElement(By.xpath("//a[contains(text(),'"+found+"')]")).getAttribute("title");
driver.findElement(By.xpath(".//*[@title='"+id+"']")).click
           


Comments

Popular posts from this blog

Website's underlying technology for automation

XPATH for IE / internet explorer

All time good software testing books!