我使用Excel VBA和Selenium自动化网页。在某个页面中,我如何检查页面中是否存在某个文本?
答案 0 :(得分:0)
您可以尝试使用XPath定位元素:
Dim drv As New FirefoxDriver
drv.Get "https://www.google.co.uk"
If drv.FindElementsByXPath("//*[contains(text(),'Google')]").Count Then
Debug.Print "Text is present"
Else
Debug.Print "Text is not present"
End If
drv.Quit
或直接检查内容:
Const JS_HAS_TEXT = "return document.body.textContent.indexOf(arguments[0]) > -1"
Dim drv As New FirefoxDriver
drv.Get "https://www.google.co.uk"
If drv.ExecuteScript(JS_HAS_TEXT, "Google") Then
Debug.Print "Text is present"
Else
Debug.Print "Text is not present"
End If