Java- code to find the earliest date + Selenium WebDriver to perform actions
Here is an interesting code that will
- Pick elements from a webpage
- Convert the text into time
- Put all the collected time into a sorted date set
- Pick the earliest date in the set
- 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
Post a Comment