Skip to content

Instantly share code, notes, and snippets.

@abdallaadelessa
Last active August 30, 2022 08:01
Show Gist options
  • Save abdallaadelessa/682fc9c2ea244764ddf5e0c2625bdf67 to your computer and use it in GitHub Desktop.
Save abdallaadelessa/682fc9c2ea244764ddf5e0c2625bdf67 to your computer and use it in GitHub Desktop.
This kotlin script opens a new google search page and search for **abdallaessa92** and then print the result stats
#!/usr/bin/env kotlin
@file:DependsOn("org.seleniumhq.selenium:selenium-java:3.141.59")
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
MainCommand.main()
object MainCommand {
private val driver: WebDriver by lazy {
System.setProperty("webdriver.chrome.whitelistedIps", "")
ChromeDriver(ChromeOptions().apply {
addArguments("--ignore-ssl-errors=yes")
addArguments("--ignore-certificate-errors")
setHeadless(true)
})
}
fun main() {
driver.get("https://google.com/")
driver.manage().window().maximize()
search("abdallaessa92")
driver.quit()
println("Finished")
}
private fun search(query: String) {
// dismiss the cookie dialog
Actions(driver)
.moveToElement(driver.findElement(By.id("zV9nZe")))
.click()
.perform()
// Enter the search query
Actions(driver)
.moveToElement(driver.findElement(By.name("q")))
.click()
.sendKeys(query)
.perform()
WebDriverWait(driver, 5)
.until { driver.findElement(By.name("q")).getAttribute("value") == query }
// Click search
Actions(driver)
.moveToElement(
WebDriverWait(driver, 5)
.until(
ExpectedConditions.elementToBeClickable(
By.xpath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]")
)
)
)
.click()
.perform()
// Print the search result stats
println(
WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By.id("result-stats"))).text
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment