SeleniumBasic ByClass 要素検索の属性をオブジェクトで生成

リファレンス

概要

要素検索の属性をオブジェクトで生成するクラス

オブジェクト設定

・変数宣言

Dim By As New By

文法

By.[メソッド](<検索値>)

メソッド・プロパティ

メソッド

スクロールできます
Name解説Short Example
Any複数の属性をor条件で利用By.Any(By.Class("MV3Tnb"), By.ID("id"))
Classクラス名属性の設定driver.FindElement(By.Class("gLFyf")).SendKeys "hoge"
CssCssセレクタ属性の設定driver.FindElement(By.Css("#redirect")).Click
Idid属性の設定driver.FindElement(By.ID("APjFqb")).SendKeys " age"
LinkTextリンクテキスト属性の設定driver.FindElement(By.Linktext("Googleについて")).Click
NameName属性の設定driver.FindElement(By.Name("q")).SendKeys " sage"
PartialLinkTextリンクテキスト(部分一致)属性の設定driver.FindElement(By.Partiallinktext("Googleにつ")).Click
Tagタグ属性の設定driver.FindElement(By.Tag("textarea")).SendKeys " warosu"
XPathXpath属性の設定driver.FindElement(By.XPath("//*[@id='APjFqb']")).SendKeys " barusu"

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

プロパティ

スクロールできます
Name解説Short Example
Strategyメソッド名の取得Debug.Print myBy.Strategy
Value設定名の取得Debug.Print myBy.Value

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

Example

Use_ByAny

Private Sub Use_ByAny()
    Dim driver As New ChromeDriver
    Dim By As New By
    driver.Get "https://www.google.co.jp"
    Dim bt
    Set bt = By.Any(By.Class("MV3Tnb"), By.ID("gbqfbb"))
    driver.FindElement(bt).Click
    Stop
    driver.Quit
End Sub

Use_ByCss

Private Sub Use_ByCss()
    Dim driver As New ChromeDriver
    Dim By As New By
    driver.Get "https://the-internet.herokuapp.com/redirector"
    Dim myBy As By
    Set myBy = By.Css("#redirect")
    driver.FindElement(myBy).Click
    Debug.Print myBy.Strategy
    Debug.Print myBy.Value
    Stop
    driver.Quit
End Sub

Use_ByLinkText

Private Sub Use_ByLinkText()
    Dim driver As New ChromeDriver
    Dim By As New By
    driver.Get "https://www.google.co.jp"
    driver.FindElement(By.linktext("Googleについて")).Click
    Stop
    driver.Quit
End Sub

Use_BypartialLinkText

Private Sub Use_BypartialLinkText()
    Dim driver As New ChromeDriver
    Dim By As New By
    driver.Get "https://www.google.co.jp"
    driver.FindElement(By.partiallinktext("Googleにつ")).Click
    Stop  
    driver.Quit
End Sub

Other_By

Private Sub Other_By()

    Dim driver As New ChromeDriver
    Dim By As New By
    driver.Get "https://www.google.co.jp"

    driver.FindElement(By.Class("gLFyf")).SendKeys "hoge"
    driver.FindElement(By.ID("APjFqb")).SendKeys " age"
    driver.FindElement(By.Name("q")).SendKeys " sage"
    
    driver.FindElement(By.XPath("//*[@id='APjFqb']")).SendKeys " barusu"
    driver.FindElement(By.Tag("textarea")).SendKeys " warosu"

    driver.Wait 2000
    driver.Quit
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

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