[VBA]SeleniumBasicのサンプル集

ExcelでWebスクレイピングの結果を出力する必要があり、SeleniumBasicを利用することとなりました。
SeleniumBasicの公式リファレンスもなかったので、自分用にGithubにあったExampleのコードから使いそうなものだけ転記してます。

[Github | florentbr/SeleniumBasic](https://github.com/florentbr/SeleniumBasic)

# Getリクエストを送る
```vb
Private Sub Handle_Access()
  Dim drv As New ChromeDriver
  drv.Get "https://www.google.co.uk"
  
  Dim ele As WebElement
  Set ele = drv.FindElementByXPath("//input[@value='Google Search']")
  Assert.Equals "Google Search", ele.Value
  
  drv.Quit
End Sub
```
# POSTリクエストを送る
```vb
Private Sub Handle_Send()
  ' API: https://code.google.com/p/selenium/wiki/JsonWireProtocol

  Dim driver As New ChromeDriver
  driver.Get "about:blank"
  
  ' Returns all windows handles
  Dim hwnds As List
  Set hwnds = driver.Send("GET", "/window_handles")
  
  ' Returns all links elements
  Dim links As List
  Set links = driver.Send("POST", "/elements", "using", "css selector", "value", "a")
  
  Debug.Assert 0
  driver.Quit
End Sub
```
# 特定のリンクを探す
```vb
Private Sub Find_By_LinkText()
  Dim drv As New ChromeDriver
  drv.Get "https://en.wikipedia.org/wiki/Main_Page"

  drv.FindElementByLinkText("Talk").Click
  Assert.Matches "User talk.*", drv.title
  
  drv.Quit
End Sub
```
# 特定の値を探す
```vb
Private Sub Find_By_Value()
  Dim drv As New ChromeDriver
  drv.Get "https://www.google.co.uk"
  
  Dim ele As WebElement
  Set ele = drv.FindElementByXPath("//input[@value='Google Search']")
  Assert.Equals "Google Search", ele.Value
  
  drv.Quit
End Sub
```
# テーブルの値をループで取得
```vb
Private Sub Handle_Table()
  Dim driver As New ChromeDriver
  driver.Get "http://the-internet.herokuapp.com/tables"
  
  'Print all cells from the second column
  Dim ele As WebElement
  For Each ele In driver.FindElementsByCss("#table1 tbody tr td:nth-child(2)")
      Debug.Print ele.Text
  Next
  
  driver.Quit
End Sub
```
# タイピングでフォームに入力
```vb
Private Sub Handle_Input()
  Dim driver As New ChromeDriver
  driver.Get "https://en.wikipedia.org/wiki/Main_Page"
  
  'get the input box
  Dim ele As WebElement
  Set ele = driver.FindElementById("searchInput")
  
  'set the text
  ele.SendKeys "abc"
  
  'get the text
  Dim txt As String
  txt = ele.Value
  
  'assert text
  Assert.Equals "abc", txt
  
  driver.Quit
End Sub
```
# スクリプトでフォームに入力
```vb
Private Sub Handle_Input_With_Script()
  Dim driver As New ChromeDriver
  driver.Get "https://en.wikipedia.org"
  
  driver.FindElementById("searchInput").ExecuteScript _
      "this.value=arguments[0];", "my value"
  
  driver.Quit
End Sub
```

# Javascriptの実行
```vb
Private Sub Execute_Script()
  Dim driver As New ChromeDriver
  driver.Get "https://en.wikipedia.org/wiki/Main_Page"
  
  Dim title
  title = driver.ExecuteScript("return document.title;")
  Debug.Assert "Wikipedia, the free encyclopedia" = title
  
  driver.Quit
End Sub
```
# 要素に対してJavascriptの実行
```vb
Private Sub Execute_Script_On_Element()
  Dim driver As New ChromeDriver
  driver.Get "https://en.wikipedia.org/wiki/Main_Page"
  
  Dim name
  name = driver.FindElementById("searchInput") _
               .ExecuteScript("return this.name;")
                
  Debug.Assert "search" = name
  
  driver.Quit
End Sub
```
# URL取得
```vb
Private Sub GetWebPage_Url()
  Dim driver As New ChromeDriver
  
  ' get the main page
  driver.Get "https://www.google.co.uk"
  Assert.Equals "https://www.google.co.uk/", driver.URL
  
  ' get a sub page
  driver.Get "/intl/en/about/"
  Assert.Equals "https://www.google.co.uk/intl/en/about/", driver.URL
  
  ' get another sub page
  driver.baseUrl = "https://www.google.co.uk/intl/en"
  driver.Get "/policies/privacy"
  Assert.Equals "https://www.google.co.uk/intl/en/policies/privacy/", driver.URL
  
  driver.Quit
End Sub
```
# 待機する
```vb
Private Assert As New Selenium.Assert
Private Waiter As New Selenium.Waiter
```
```vb
Private Sub Should_Wait_For_Delegate()
  Dim driver As New ChromeDriver

  ' without delegate
  While Waiter.Not(WaitDelegate1(), timeout:=2000): Wend
  
  ' without delegate with argument
  While Waiter.Not(WaitDelegate2(driver), timeout:=2000): Wend

  ' with delegate on the driver
  driver.Until AddressOf WaitDelegate1, timeout:=2000
  
  ' with delegate with argument
  Waiter.Until AddressOf WaitDelegate1, driver, timeout:=2000
  
  ' with delegate without argument
  Waiter.Until AddressOf WaitDelegate2, timeout:=2000
End Sub
```
```vb
Private Function WaitDelegate1()
  WaitDelegate1 = True
End Function
```