1. 程式人生 > >WebDriver 的協議標準 W3C

WebDriver 的協議標準 W3C

Selenium WebDriver 快速入門工程:

https://github.com/Jason-Chen-2017/go-selenium

import org.openqa.selenium.By
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.CapabilityType
import org.openqa.selenium.remote.DesiredCapabilities
import java.lang.Thread.sleep
import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {
    val driver = initDriver()

    try {
        driver.get("https://www.baidu.com/")
        driver.findElement(By.id("kw")).click()
        driver.findElement(By.id("kw")).sendKeys("Kotlin")
        driver.findElement(By.id("su")).click()

        sleep(3000)

        // 百度搜索 Kotlin,自動翻10頁
        for (i in 1..10) {
            driver.executeScript("scrollTo(0,2000)")
            sleep(3000)
            val e = driver.findElementByPartialLinkText("""下一頁""")
            e.click()
        }

        sleep(7000)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    driver.quit()
}

private fun initDriver(): ChromeDriver {
    System.setProperty("webdriver.chrome.driver", "chromedriver")// 設定 webdriver 路徑到系統環境變數中
    val capabilities = DesiredCapabilities.chrome()
    // SSL Certificate
    capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true)
    capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true)

    val headless = false
    var driver = ChromeDriver(chromeOptions(headless))
    val sessionId = driver.sessionId
    println("sessionId=${sessionId}")
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)
    return driver
}


fun chromeOptions(headless: Boolean): ChromeOptions {
    val options = ChromeOptions()
    if (headless) {
        options.addArguments("headless")
    }
    return options
}

WebDriver W3C 標準協議:

https://w3c.github.io/webdriver/

Method URI Template Command
POST /session New Session
DELETE /session/{session id} Delete Session
GET /status Status
GET /session/{session id}/timeouts Get Timeouts
POST /session/{session id}/timeouts Set Timeouts
POST /session/{session id}/url Navigate To
GET /session/{session id}/url Get Current URL
POST /session/{session id}/back Back
POST /session/{session id}/forward Forward
POST /session/{session id}/refresh Refresh
GET /session/{session id}/title Get Title
GET /session/{session id}/window Get Window Handle
DELETE /session/{session id}/window Close Window
POST /session/{session id}/window Switch To Window
GET /session/{session id}/window/handles Get Window Handles
POST /session/{session id}/frame Switch To Frame
POST /session/{session id}/frame/parent Switch To Parent Frame
GET /session/{session id}/window/rect Get Window Rect
POST /session/{session id}/window/rect Set Window Rect
POST /session/{session id}/window/maximize Maximize Window
POST /session/{session id}/window/minimize Minimize Window
POST /session/{session id}/window/fullscreen Fullscreen Window
GET /session/{session id}/element/active Get Active Element
POST /session/{session id}/element Find Element
POST /session/{session id}/elements Find Elements
POST /session/{session id}/element/{element id}/element Find Element From Element
POST /session/{session id}/element/{element id}/elements Find Elements From Element
GET /session/{session id}/element/{element id}/selected Is Element Selected
GET /session/{session id}/element/{element id}/attribute/{name} Get Element Attribute
GET /session/{session id}/element/{element id}/property/{name} Get Element Property
GET /session/{session id}/element/{element id}/css/{property name} Get Element CSS Value
GET /session/{session id}/element/{element id}/text Get Element Text
GET /session/{session id}/element/{element id}/name Get Element Tag Name
GET /session/{session id}/element/{element id}/rect Get Element Rect
GET /session/{session id}/element/{element id}/enabled Is Element Enabled
POST /session/{session id}/element/{element id}/click Element Click
POST /session/{session id}/element/{element id}/clear Element Clear
POST /session/{session id}/element/{element id}/value Element Send Keys
GET /session/{session id}/source Get Page Source
POST /session/{session id}/execute/sync Execute Script
POST /session/{session id}/execute/async Execute Async Script
GET /session/{session id}/cookie Get All Cookies
GET /session/{session id}/cookie/{name} Get Named Cookie
POST /session/{session id}/cookie Add Cookie
DELETE /session/{session id}/cookie/{name} Delete Cookie
DELETE /session/{session id}/cookie Delete All Cookies
POST /session/{session id}/actions Perform Actions
DELETE /session/{session id}/actions Release Actions
POST /session/{session id}/alert/dismiss Dismiss Alert
POST /session/{session id}/alert/accept Accept Alert
GET /session/{session id}/alert/text Get Alert Text
POST /session/{session id}/alert/text Send Alert Text
GET /session/{session id}/screenshot Take Screenshot
GET /session/{session id}/element/{element id}/screenshot Take Element Screenshot