Skip to content

Instantly share code, notes, and snippets.

@Kinetic27
Created August 24, 2019 10:31
Show Gist options
  • Save Kinetic27/c605ef47bf54a97d6f00349c9afe4ebc to your computer and use it in GitHub Desktop.
Save Kinetic27/c605ef47bf54a97d6f00349c9afe4ebc to your computer and use it in GitHub Desktop.
크롬 드라이버 귀찮은 코드들 도우미 Main.kt는 예제.
/**
* @since 2018-08-24.
* @author Kinetic(@dev_kinetic)
*/
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
import java.util.*
import java.util.concurrent.TimeUnit
// 인풋 귀찮아서 코드에 박아둡니다
fun input(msg: String = ""): Any = with(Scanner(System.`in`)) {
print(msg)
next()
}
fun ChromeDriver.scrollToElement(element: WebElement, alignWithTop: Boolean = false) {
executeScript("arguments[0].scrollIntoView($alignWithTop);", element)
}
fun ChromeDriver.scrollDown(scrollY: String = "document.body.scrollHeight") {
executeScript("window.scrollTo(0, $scrollY);")
}
fun ChromeDriver.loadWithDelay(url: String, delay: Long) {
get(url)
manage().timeouts().implicitlyWait(delay, TimeUnit.MINUTES)
}
// Webelement 반환함
fun ChromeDriver.waitUntil(by: By, delay: Long): WebElement? =
WebDriverWait(this, delay).until(
ExpectedConditions.presenceOfElementLocated(by)
)
import org.openqa.selenium.chrome.ChromeDriver
fun main(args: Array<String>) {
val chromeDriverLocation = "src/chromedriver.exe"
System.setProperty("webdriver.chrome.driver", chromeDriverLocation) // 크롬 드라이버 경로 지정
with(ChromeDriver()) {
// ==================== 네이버 맞춤법 검사기 ====================
loadWithDelay(
"https://search.naver.com/search.naver?query=맞춤법검사기",
5
)
val str = input("맞춤법 검사를 원하는 단어나 문장을 입력해주세요. : ") as? String ?: "Error"
findElementByCssSelector("div > textarea").sendKeys(str)
findElementByCssSelector("button[class=btn_check]").click()
findElementByCssSelector("button[class=copy]").click()
println(
"""
|
|검사결과가 복사되었습니다.
|원하는 곳에 붙여넣기(Ctrl+V)해주세요
|""".trimMargin()
)
scrollDown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment