《SeleniumBasic 3.141.0.0 - 在VBA中操作瀏覽器》系列文章之四:元素定位


SeleniumBasic的IWebDriver和IWebElement兩個對象下面,都有如下16個函數,用於定位其他元素。

  1. Function FindElementByClassName(className As String) As IWebElement
  2. Function FindElementByCssSelector(cssSelector As String) As IWebElement
  3. Function FindElementById(id As String) As IWebElement
  4. Function FindElementByLinkText(linkText As String) As IWebElement
  5. Function FindElementByName(Name As String) As IWebElement
  6. Function FindElementByPartialLinkText(partialLinkText As String) As IWebElement
  7. Function FindElementByTagName(tagName As String) As IWebElement
  8. Function FindElementByXPath(xpath As String) As IWebElement
  9. Function FindElementsByClassName(className As String) As IWebElement()
  10. Function FindElementsByCssSelector(cssSelector As String) As IWebElement()
  11. Function FindElementsById(id As String) As IWebElement()
  12. Function FindElementsByLinkText(linkText As String) As IWebElement()
  13. Function FindElementsByName(Name As String) As IWebElement()
  14. Function FindElementsByPartialLinkText(partialLinkText As String) As IWebElement()
  15. Function FindElementsByTagName(tagName As String) As IWebElement()
  16. Function FindElementsByXPath(xpath As String) As IWebElement()

其中,前面8個函數返回的結果是一個網頁元素,后面8個都是FindElements開頭的函數,返回的是多個元素形成的數組。

用於定位的主對象可以是瀏覽器,也可以是一個已有的網頁元素。

下面以定位百度首頁為例,代碼片段如下:

    WD.New_ChromeDriver Service:=Service, Options:=Options
    WD.URL = "https://www.baidu.com"
    Dim form As SeleniumBasic.IWebElement
    Dim inputs() As IWebElement
    Dim keyword As SeleniumBasic.IWebElement
    Dim button As SeleniumBasic.IWebElement
    Set form = WD.FindElementById("form")
    Set keyword = form.FindElementById("keyword")
    Debug.Print keyword Is Nothing
    inputs = form.FindElementsByTagName(tagName:="input")
    Debug.Print UBound(inputs)
    Dim i As Integer
    For i = 0 To UBound(inputs)
        Debug.Print inputs(i).tagName
    Next i
    Set keyword = form.FindElementById("kw")
    keyword.Clear
    keyword.SendKeys "好看視頻"
    Set button = form.FindElementById("su")
    button.Click

form是由瀏覽器對象WD作為主對象定位到的,其他的比如keyword,button、inputs這幾個則是從form開始定位的。

注意代碼中的inputs是一個數組,只要是FindElements系列的函數都可以賦給它。在運行過程中,可以打開本地窗口看到該數組中每個元素。

下面再講一下如何判斷元素的有無。

在網頁自動化過程中,判斷某個特征的元素是否存在,是相當重要的。SeleniumBasic的FindElement系列方法,當定位到一個元素時返回該元素,定位不到時返回Nothing。

    Dim button As SeleniumBasic.IWebElement
    Set button = form.FindElementById("submit")
    If button Is Nothing Then
        Debug.Print "沒有找到元素。"
    Else
        button.Click
    End If

另外,FindElements系列方法,當定位到一個以上的元素時,返回元素數組。如果一個也定位不到則該數組的UBound是-1。

    Dim buttons() As IWebElement    
    buttons = form.FindElementsById("su2")
    Debug.Print IsEmpty(buttons)
    If UBound(buttons) = -1 Then
        Debug.Print "沒有找到任何元素。"
    Else
        Debug.Print "找到的元素個數為", UBound(buttons) + 1
    End If

注意代碼中,使用IsEmpty判斷哪一行,本來沒什么作用,但是不寫是不行的,這可能是微軟的一個問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM