SeleniumBasic WebElementClass Web要素の制御

リファレンス

概要

Web要素制御用のクラス

オブジェクト設定

・変数宣言

Dim ele As WebElement

文法

Set ele = driver.[SerchContext]
ele.[メソッド]

メソッド・プロパティ

メソッド

スクロールできます
Name解説Short Example
AsSelectWebElement を Select 要素に代入Set ele = Driver.FindElementById("dropdown").AsSelect
AsTableWebElement を Table 要素に代入driver.FindElementById("table1").AsTable.ToExcel
Attribute属性値を取得Debug.Print ele.Attribute("textContent")
Clearテキスト入力要素の場合、テキストをクリアele.Clear
Click要素をクリックele.Click
ClickAndHold要素クリックを保持 
ClickByOffset取得要素からX,Y軸指定で移動し、クリックele.ClickByOffset 210,0
ClickContext要素を右クリックele.ClickContext
ClickDouble要素をダブルクリックele.ClickDouble
CssValueCssプロパティを返すDebug.Print ele.CssValue("font-style")
DragAndDropToOffset要素のドラッグ&ドロップ
(向先xy軸指定)
 
DragAndDropToWebElement要素のドラッグ&ドロップ
(向先要素指定)
 
Equals(WebElement)指定Web要素と一致するか判定Debug.Print ele.Equals(ele2)
ExecuteAsyncScriptJavaScriptコードを非同期で実行 
ExecuteScriptJavaScriptコードの実行ele.ExecuteScript JS_ADD_YELLOW_BORDER
HoldKeysキー押下を保持ele.HoldKeys Keys.Control
Locationレンダリング可能なキャンバス内の要素の位置を返すDebug.Print ele.Location.X & " : " & ele.Location.Y
LocationInView対象の要素が表示可能な位置までスクロールし、要素の位置を返す。Debug.Print ele.LocationInView.X & " : " & ele.LocationInView.Y
ReleaseKeysキーボードを離す 
ReleaseMouseマウスを離す 
ScrollIntoView要素をブラウザ ウィンドウの表示領域にスクロールele.ScrollIntoView
SendKeysキーボード入力操作ele.SendKeys "V"
Size要素のサイズを返すDebug.Print elm.Size().Height
Debug.Print elm.Size().Width
Submitフォームを送信ele.Submit
TakeScreenshot現在の要素のスクリーンショットを取得Set img = elm.TakeScreenshot()
Text要素のテキスト取得Debug.Print ele.Text
TextAsNumber要素のテキストから抽出した数値を返すDebug.Print Driver.FindElementByCss("#frame-interlang").TextAsNumber
TextMatch指定された入力文字列を検索して、パターン パラメーターで指定された正規表現が出現するかどうかを調べる 
TextMatches正規表現に一致するすべての出現を返します 
Untilデリゲート関数がnot null ot true 以外を返すのを待機 
ValueValue属性を返す
WaitAttribute属性を指定して合致すれば通過ele.WaitAttribute "textContent", "コモンズ", 10000
WaitCssValueCssプロパティを待機ele.WaitCssValue "font-style", "normal", 5000
WaitDisplayed要素が表示されるのを待機ele.WaitDisplayed
WaitEnabled要素が有効になるまで待機ele.WaitEnabled
WaitForScriptスクリプトが true or not null を返すのを待機 
WaitNotAttribute別の属性を待機ele.WaitNotAttribute "textContent", "MediaWiki", 2000
WaitNotCssValue別のcssプロパティを待機ele.WaitNotCssValue "font-style", "hoge", 2000
WaitNotText要素テキストに該当しなければ通過ele.WaitNotText "hoge", 2000
WaitRemovalWeb 要素が DOM から削除されるまで待機ele.WaitRemoval 2000
WaitSelection要素が選択(True)or非選択(False)になるのを待機(デフォルトはTrue)ele.WaitSelection True, 2000
WaitText要素テキストに該当すれば通過(部分一致検索が可能)ele.WaitText "コモ", 10000

※Short Exampleは、動作確認ができたコードを記載しています。

メソッド(SearchContextからのクラス継承)


WebElement中のSearchContextとなるため、文法は以下表現になります。
driver.[SerchContext].[SerchContext]
FindeElementで親ノードの要素を取得した後、子ノードの要素を取得したい時に利用できます。
コードとしては、FindElementを2回利用している感じになります。
driver.[SerchContext]の利用方法は、こちらを参照ください。

スクロールできます
Name解説ShortExample
FindElement指定メソッドを使用して最初の Web要素 を検索、取得
FindElementByByクラスを利用し、Web要素検索、取得
FindElementByClassCSSクラスの値でWeb要素検索、取得Debug.Print ele.FindElementByClass("header").Text
FindElementByCssCSSセレクタの値でWeb要素検索、取得Debug.Print ele.FindElementByCss("thead").Text
FindElementByIdIdの値でWeb要素検索、取得
FindElementByLinkTextリンクテキストでWeb要素検索、取得(完全一致)Debug.Print ele.FindElementByLinkText("edit").Text
FindElementByNameNameの値でWeb要素検索、取得
FindElementByPartialLinkTextリンクテキストでWeb要素検索、取得(部分一致)Debug.Print ele.FindElementByPartialLinkText("ed").Text
FindElementByTagTag名でWeb要素検索、取得 
FindElementByXPathXPathWeb要素検索、取得 
FindElements指定メソッドを使用して検索ヒットしたWeb要素を全て取得 
FindElementsByByクラスで検索ヒットしたWeb要素を全て取得 
FindElementsByClassCSSクラスの値で検索ヒットしたWeb要素を全て取得 
FindElementsByCssCSSセレクタの値で検索ヒットしたWeb要素を全て取得ele.FindElementsByCss("tbody tr td:nth-child(2)")
FindElementsByIdIdの値で検索ヒットしたWeb要素を全て取得 
FindElementsByLinkTextリンクテキストで検索ヒットしたWeb要素を全て取得(完全一致) 
FindElementsByNameNameの値で検索ヒットしたWeb要素を全て取得 
FindElementsByPartialLinkTextリンクテキストで検索ヒットしたWeb要素を全て取得(部分一致) 
FindElementsByTagTag名で検索ヒットしたWeb要素を全て取得 
FindElementsByXPathXpath名で検索ヒットしたWeb要素を全て取得Set eles = eles_base.Item(1).FindElementsByXPath("//*[@id='table1']/tbody//td[2]")
IsElementPresent指定のWeb要素がページ内に存在するか確認 
WaitNotElement要素が無くなるのを待機 

※Short Exampleは、動作確認ができたコードを記載しています。

プロパティ

スクロールできます
Name解説Short Example
IsDisplayed要素が表示されるかどうかTrue or Falseを返す。 
取得した要素がtype="hidden" の場合、Falseを返す。
Debug.Print "IsDisplayed:" & ele.IsDisplayed
IsEnabled要素が有効かどうかTrue or Falseを返す。
取得した要素がdisabled="true"の場合、Falseを返す。
Debug.Print "IsEnabled:" & ele.IsEnabled
IsPresent要素が存在かどうかTrue or Falseを返すDebug.Print "IsPresent:" & ele.IsPresent
IsSelected要素が選択されているかどうかTrue or Falseを返す。
取得した要素がSelect属性の場合、Trueを返す。
Debug.Print "IsSelected:" & ele.IsSelected
TagName要素のタグ名を取得Debug.Print ele.tagname

※Short Exampleは、動作確認ができたコードを記載しています。

Example

Find_Element_input

Private Sub Find_Element_input()
    Dim Driver As New ChromeDriver
    Driver.Get "https://www.google.co.jp"
    Dim ele As WebElement, ele2 As WebElement
    Set ele = Driver.FindElementByName("q")
    Set ele2 = Driver.FindElementByName("q")
    Debug.Print ele.Equals(ele2)

    ele.SendKeys "Eiffel tower"
    Driver.Wait 1000
    ele.Clear
    Driver.Wait 2000
    
'    ele.SendKeys "Eiffel tower" & Driver.Keys.Return
    ele.SendKeys "Eiffel tower", Driver.Keys.Return
        
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_WaitMethod

Private Sub Use_WaitMethod()

    Dim Driver As New ChromeDriver
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Dim ele As WebElement, str As String
    str = "#sister-projects-list > div:nth-child(1) > div:nth-child(2) > b > a"
    Set ele = Driver.FindElementByCss(str)
    
    ele.WaitAttribute "textContent", "コモンズ", 10000
    ele.WaitText "コモ", 10000
    ele.WaitCssValue "font-style", "normal", 5000
    
    ele.WaitNotAttribute "textContent", "MediaWiki", 2000
    ele.WaitNotCssValue "font-style", "hoge", 2000
    ele.WaitNotText "hoge", 2000
    
    ele.WaitRemoval 2000
    Driver.Quit

End Sub

Use_Attribute

Private Sub Use_Attribute()
    Dim Driver As New ChromeDriver
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Dim ele As WebElement, str As String
    str = "#sister-projects-list > div:nth-child(1) > div:nth-child(2) > b > a"
    Set ele = Driver.FindElementByCss(str)
    
    Debug.Print ele.Attribute("textContent")
    Debug.Print ele.CssValue("font-style")
    Debug.Print ele.Text
    Debug.Print Driver.FindElementByCss("#frame-interlang").TextAsNumber
    
    Set ele = Driver.FindElementByCss(str).WaitAttribute("textContent", "コモンズ", 10000)
    ele.WaitAttribute "textContent", "コモンズ", 10000
    ele.WaitText "コモ", 10000
    
    Dim i As Long
    For i = 0 To 500 Step 10
        ele.ClickByOffset 100 + i, 0
        If Not ele.IsPresent Then Exit For
    Next i
    
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_Click

Private Sub Use_Click()
    Dim Driver As New ChromeDriver
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Dim ele As WebElement, str As String
    str = "#sister-projects-list > div:nth-child(1) > div:nth-child(2) > b > a"
    Set ele = Driver.FindElementByCss(str)
    
    ele.ClickContext
    ele.ClickDouble
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_Location

Private Sub Use_Location()
    Dim Driver As New selenium.ChromeDriver
    Dim ele As WebElement
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Set ele = Driver.FindElementByCss("#sister-projects-list > div:nth-child(1) > div:nth-child(2) > b > a")
    Debug.Print ele.Location.X & " : " & ele.Location.Y
    Debug.Print ele.LocationInView.X & " : " & ele.LocationInView.Y
    ele.ScrollIntoView
    Driver.Wait 2000
    Driver.Quit
End Sub

Usage_ClipBoard

Private Sub Usage_ClipBoard()
    Dim Keys As New selenium.Keys
    Dim Driver As New selenium.ChromeDriver
    Dim ele As WebElement
    Driver.Get "https://google.co.jp"
    Driver.SetClipBoard "いろはにほへと"
    Set ele = Driver.FindElementByName("q")
    ele.HoldKeys Keys.Control
    ele.SendKeys "V"
    Driver.Wait 2000
    ele.ReleaseKeys Keys.Control  'リリース不可
    Driver.Wait 2000
    ele.SendKeys "V"
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_Size

Private Sub Use_Size()
    Dim Driver As New ChromeDriver
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Dim img As selenium.Image
    Dim elm As selenium.WebElement
    Set elm = Driver.FindElementByXPath("//*[@id='sister-projects-list']")
    Driver.Actions.MoveToElement(elm).Perform
    Debug.Print "Height:" & elm.Size().Height
    Debug.Print "Width:" & elm.Size().Width
    Driver.Quit
End Sub

Use_property

Private Sub Use_property()
    Dim Driver As New ChromeDriver
    Driver.Get "https://ja.wikipedia.org/wiki/メインページ"
    Dim ele As WebElement, str As String
    str = "#sister-projects-list > div:nth-child(1) > div:nth-child(2) > b > a"
    Set ele = Driver.FindElementByCss(str)
    Debug.Print "IsDisplayed:" & ele.IsDisplayed
    Debug.Print "IsEnabled:" & ele.IsEnabled
    Debug.Print "IsPresent:" & ele.IsPresent
    Debug.Print "IsSelected:" & ele.IsSelected
    Debug.Print ele.tagname
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_IsDisplay

Private Sub Use_IsDisplay()
    Dim Driver As New ChromeDriver
    Driver.Get "https://google.co.jp"
    Dim ele As WebElement, str As String
    Set ele = Driver.FindElementByCss("#tophf > input[type=hidden]:nth-child(1)")    'type = hidden
    ele.WaitDisplayed False, 2000
    Debug.Print "IsDisplayed:" & ele.IsDisplayed
    Debug.Print "IsPresent:" & ele.IsPresent
    Debug.Print ele.tagname
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_IsEnabled

Private Sub Use_IsEnabled()
    Dim Driver As New ChromeDriver
    Driver.Get "https://itsakura.com/js-disabled#s2"
    Dim ele As WebElement, str As String
    Set ele = Driver.FindElementByCss("#b2")
    Debug.Print "IsEnabled:" & ele.IsEnabled
    Driver.FindElementByCss("#btn2").Click
    ele.WaitEnabled False, 2000
    Debug.Print "IsEnabled:" & ele.IsEnabled
    Debug.Print "IsPresent:" & ele.IsPresent
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_IsSelected

Private Sub Use_IsSelected()
    Dim Driver As New ChromeDriver
    Driver.Get "https://digitor.jp/textbook/html-pull-down/"
    Dim ele As WebElement, str As String
    Set ele = Driver.FindElementByXPath("/html/body/div[4]/div[2]/div[1]/div[5]/select/option[3]")

    Debug.Print "IsSelected:" & ele.IsSelected
    ele.Click
    ele.WaitSelection True, 2000
    Debug.Print "IsSelected:" & ele.IsSelected
    
    Driver.Wait 2000
    Driver.Quit
End Sub

Use_WebEle_findEle

Private Sub Use_WebEle_findEle()
    Dim driver As New ChromeDriver
    Dim ele As WebElement
    driver.Get "https://the-internet.herokuapp.com/tables"
    Set ele = driver.FindElementById("table1")
    Debug.Print ele.FindElementByClass("header").Text
    Debug.Print ele.FindElementByCss("thead tr th:nth-child(1)").Text
    Debug.Print ele.FindElementByCss("thead").Text
    Debug.Print ele.FindElementByLinkText("edit").Text
    Debug.Print ele.FindElementByPartialLinkText("ed").Text
    Stop
    driver.Quit
End Sub

Use_FindEles_Css

Private Sub Use_FindEles_Css()
    Dim driver As New ChromeDriver
    driver.Get "https://the-internet.herokuapp.com/tables"
    Dim eles As WebElements, ele As WebElement, buf As WebElement
    Set ele = driver.FindElementById("table1")
    Set eles = ele.FindElementsByCss("tbody tr td:nth-child(2)")
    For Each buf In eles
    Debug.Print buf.Text
    Next buf
    driver.Quit
End Sub

Use_FindEles_XPath

Private Sub Use_FindEles_XPath()
    Dim driver As New ChromeDriver
    driver.Get "https://the-internet.herokuapp.com/tables"
    Dim eles_base As WebElements, eles As WebElements, ele As WebElement
    Set eles_base = driver.FindElementsByClass("tablesorter")
    Set eles = eles_base.Item(1).FindElementsByXPath("//*[@id='table1']/tbody//td[2]")
    For Each ele In eles
    Debug.Print ele.Text
    Next ele
    driver.Quit
End Sub
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

VBAを中心とした自動化、効率化の手法を紹介しています。現在は、SeleniumBasicのexamplesを紹介しています。その内、SeleniumBasic以外の手法も掲載したいと思っております。