WebDriver 定义了一个简洁的界面来访问网页并与之交互,所有的动作和行为都经过优化,尽可能地模仿真人,比如滚动、点击、键入文本、拖放等。
该接口中的方法主要分为三类:
最主要的方法有:
class WebDriverDemo(private val session: PulsarSession) {
    private val logger = LoggerFactory.getLogger(this.javaClass)
    private val searchBoxSelector = ".form input[type=text]"
    private val searchBoxSubmit = ".form input[type=submit]"
    val fieldSelectors = mutableMapOf(
        "title" to "#productTitle",
        "reviews" to "#acrCustomerReviewText",
        "prodDetails" to "#prodDetails"
    )
    fun options(args: String): LoadOptions {
        val options = session.options(args)
        val be = options.event.browseEventHandlers
        be.onDocumentActuallyReady.addLast { page, driver ->
            fieldSelectors.values.forEach { interact1(it, driver) }
        }
        be.onDocumentActuallyReady.addLast { page, driver ->
            interact2(driver)
        }
        be.onDidInteract.addLast { page, driver ->
            logger.info("Did the interaction")
        }
        return options
    }
    private suspend fun interact1(selector: String, driver: WebDriver) {
        if (driver.exists(selector)) {
            println("click $selector ...")
            driver.click(selector)
            println("select first text by $selector ...")
            var text = driver.firstText(selector) ?: "no-text"
            text = text.substring(1, 4)
            println("type $text in $searchBoxSelector ...")
            driver.type(searchBoxSelector, text)
        }
    }
    private suspend fun interact2(driver: WebDriver) {
        val selector = "#productTitle"
        println("bring the page to front ...")
        driver.bringToFront()
        println("scroll to the bottom of the page ...")
        driver.scrollToBottom()
        println("bounding box of body: " + driver.boundingBox("body"))
        println("scroll to the middle of the page ...")
        driver.scrollToMiddle(0.5f)
        println("click $selector ...")
        driver.click(selector)
        println("query text of $selector ...")
        var text = driver.firstText(selector) ?: "no-text"
        text = text.substring(1, 4)
        println("type `$text` in $searchBoxSelector")
        driver.type(searchBoxSelector, text)
        println("capture screenshot over $selector ...")
        driver.captureScreenshot(selector)
        println("evaluate 1 + 1 ...")
        val result = driver.evaluate("1 + 1")
        require(result is Int)
        println("evaluate 1 + 1 returns $result")
        println("wheel down for 5 times ...")
        driver.mouseWheelDown(5, delayMillis = 2000)
        println("scroll to top ...")
        driver.mouseWheelDown(5, delayMillis = 2000)
        driver.scrollToTop()
        println("search ...")
        text = "Vincent Willem van Gogh"
        println("type `$text` in $searchBoxSelector")
        driver.type(searchBoxSelector, text)
        driver.click(searchBoxSubmit)
        val url = driver.currentUrl()
        println("the page navigated to $url")
    }
}