import org.openqa.selenium.WebDriver import org.openqa.selenium.chrome.ChromeDriver import org.openqa.selenium.By import org.openqa.selenium.Keys import org.openqa.selenium.support.ui.WebDriverWait import org.openqa.selenium.support.ui.ExpectedConditions System.setProperty("webdriver.chrome.driver", "c:/u/chromedriver.exe") val chdr = ChromeDriver() val HOST = "https://www.sports-tracker.com/login" val wait = WebDriverWait(chdr, 10) chdr.get(HOST) chdr.findElement(By.id("username")).sendKeys("kkwoo") chdr.findElement(By.id("password")).sendKeys("m0rujamn", Keys.ENTER) // wait on a particular element: https://stackoverflow.com/questions/20009211/getting-selenium-to-pause-for-x-seconds // http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[text()='Menu']"))).click() // how to find button via xpath: https://stackoverflow.com/questions/27529967/how-to-find-button-element-with-webdriver // chdr.findElement(By.xpath("//button/span[text()='Menu']")).click() wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Diary']"))).click() wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='List']"))).click() // chdr.findElement(By.xpath("//*[text()='List']")).click() // needs explicit wait // TODO: repeatedly click "Show more" until there's no more to show wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Show more']"))).click() val all_workouts = chdr.findElements(By.xpath("//a[contains(@href, '/workout/kkwoo/')]")) val some_workouts = all_workouts.take(50) some_workouts.forEach { // opens in new window, not prone to screen position changes like it.click() it.sendKeys(Keys.ENTER) // convert getWindowHandles set object to arraylist chdr.switchTo().window(ArrayList(chdr.getWindowHandles()).get(1)) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Edit']"))).sendKeys(Keys.ENTER) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Export']"))).sendKeys(Keys.ENTER) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Cancel']"))).sendKeys(Keys.ENTER) // close tab and return to main list // chdr.close() chdr.switchTo().window(ArrayList(chdr.getWindowHandles()).get(0)) } some_workouts.forEach { it.sendKeys(Keys.ENTER) chdr.switchTo().window(ArrayList(chdr.getWindowHandles()).get(1)) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Edit']"))).sendKeys(Keys.ENTER) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Export']"))).sendKeys(Keys.ENTER) wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Cancel']"))).sendKeys(Keys.ENTER) chdr.close() chdr.switchTo().window(ArrayList(chdr.getWindowHandles()).get(0)) } // TODO: repeatedly click "Show more" until there's no more to show chdr.findElement(By.xpath("//*[text()='Show more']")).click() // xpath contains function with selenium: https://stackoverflow.com/questions/13124942/xpath-for-href-element chdr.findElements(By.xpath("//a[contains(@href, '/workout/kkwoo/')]")) // FYI: // innerHTML for investigation: https://stackoverflow.com/questions/7263824/get-html-source-of-webelement-in-selenium-webdriver-using-python // >>> val button_list = chdr.findElements(By.xpath("//button")) // >>> button_list.forEach {println(it.getAttribute("innerHTML"))} // logout and exit chdr.findElement(By.partialLinkText("Logout")).click() chdr.quit()