LoginSignup
0
1

More than 5 years have passed since last update.

Windows 10 Pro x64 + Excel2016 x86 VBA で Seleniumbasic + chromedriver を使ってみる

Last updated at Posted at 2019-02-15

目的

pythonで試したコードを Excel VBA で書き直してみる
※TODO

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.NAME, "q"))
)

上記のコードは driver.Until で書くのかな??
ここをsleepで待つのだけは避けたい(Examples以下は全部見てない)

インストール

Seleniumbasicの Release page より SeleniumBasic-2.0.9.0.exe をDLする
exeを実行すると、C:\Users\user_name\AppData\Local\SeleniumBasic にインストールされる
ChromeDriver - WebDriver for Chrome より使用中のChromeのバージョンに合わせてDLする
chromedriver_win32.zip を解凍後 chromedriver.exe は SeleniumBasic フォルダに上書きする

Excel VBA で使用する

VBAのIDEの tool -> 参照設定より C:\Users\user_name\AppData\Local\SeleniumBasic\Selenium32.tlb を追加する

サンプルコード

操作する要素は最小限に切り詰めています
※Pythonのコードはどうなのかいな?

Sub ChromeDriver2()

    Dim driver As New ChromeDriver

    driver.Start
    driver.Get "http://www.google.com/xhtml"
    driver.FindElementByName("q").SendKeys ("ChromeDriver")

    Sleep 3000
    driver.Quit

End Sub

Sub ChromeDriver3()
'
    Dim driver  As New ChromeDriver
    Dim elm     As WebElement
'
    driver.Get "http://example.selenium.jp/reserveApp/"
'
    '年 を入力する
    Set elm = driver.FindElementByName("reserve_y")
    Debug.Print elm.Attribute("value")
    'textbox に入力する clear() -> send_keys()
    elm.Clear
    elm.SendKeys ("2020")
    Debug.Print elm.Attribute("value")
'
    ' radio button をクリックする
    Set elm = driver.FindElementById("breakfast_off")
    Debug.Print ("breakfast_off : " & IIf(elm.Attribute("checked"), "on", "off"))
    ' breakfast_off をクリック
    elm.Click
    Debug.Print ("clicked")
    Debug.Print ("breakfast_off : " & IIf(elm.Attribute("checked"), "on", "off"))

    ' checkbox をクリックする
    Set elm = driver.FindElementById("plan_b")
    Debug.Print "plan_b:" & IIf(elm.IsSelected, "on", "off")
    elm.Click
    Debug.Print ("clicked")
    Debug.Print "plan_b:" & IIf(elm.IsSelected, "on", "off")
'
    Sleep 1000
    driver.Quit
End Sub

Sub ChromeDriver4()
'
    Dim driver  As New ChromeDriver
    Dim elm     As WebElement
    Dim lst     As List
'
    driver.Get "http://192.168.5.48:8088/"
    ' 検索ワードの入力
    driver.FindElementById("contentQuery").SendKeys ("Postgres")

    ' 検索ボタンクリック
    driver.FindElementById("searchButton").Submit

    ' 検索結果を取得する
    Set lst = driver.FindElementsByClass("link")

    ' 検索結果を表示する
    For Each elm In lst
        Debug.Print elm.Attribute("href")
    Next
    Sleep 1000
    driver.Quit
End Sub
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