LoginSignup
0
1

More than 1 year has passed since last update.

[VBA]SeleniumBasicのサンプル集

Posted at

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

Github | florentbr/SeleniumBasic

Getリクエストを送る

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リクエストを送る

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

特定のリンクを探す

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

特定の値を探す

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

テーブルの値をループで取得

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

タイピングでフォームに入力

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

スクリプトでフォームに入力

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の実行

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の実行

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取得

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

待機する

Private Assert As New Selenium.Assert
Private Waiter As New Selenium.Waiter
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
Private Function WaitDelegate1()
  WaitDelegate1 = True
End Function
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1