问题
I want to count the numbers of elements aiming to get all their names and store in an array.
The names are highlighted in this image
The names are store in "js-list list-wrapper" as shown in image
My code:
Public Sub seleniumtutorial()
Dim bot As New SeleniumWrapper.WebDriver
Dim a As WebElement
Dim b As WebElement
Dim x() As Integer
bot.Start "chrome", "https://trello.com/login"
bot.get "/"
bot.Type "name=user", "biaverly@id.uff.br"
bot.Type "name=password", "carambola69"
bot.clickAndWait "id=login"
bot.findElementByLinkText("Gestão de Pessoas").Click
I tried:
Set a = bot.findElementsByClassName("board-canvas", 5).Count
and find elements by xpath,and id.
回答1:
Caveat: The below is for selenium basic vba. This uses the Selenium type library and is based on @florentbr's development from here. The syntax you are using makes me think the question potentially is wrongly tagged as the syntax, for the most part, is for selenium IDE. Not sure if you are converting a script? I don't recall VBA being a supported language for the IDE but......
Trello headers will be class name list-header-name
.
I would use faster .FindElementsByCss
So,
bot.FindElementsByCss(".list-header-name")
The "." before list-header-name
is a css class selector. Modern browsers are optimized for this.
The ,5
is the value for the minimum
argument; as you haven't used named arguments, matching will be positional. The third argument would be timeout
. Both minimum
and timeout
are optional.
Now, when using Set
keyword this is for assigning to an object variable:
Dim titles As Object
Set titles = bot.FindElementsByCss(".list-header-name")
You do not attempt to retrieve a property at the same time. Attempting:
Set titles = bot.FindElementsByCss(".list-header-name").Count
should lead to a type mismatch error.
You can separately take the webElement count from the collection with
Dim numberOfWebElements As Long
numberOfWebElements = titles.Count
For trello there are ids you can use for login. I would use those and SendKeys method
Outline example:
Option Explicit
Public Sub SeleniumTutorial()
Dim bot As WebDriver, titles As Object, i As Long
Dim numberOfWebElements As Long, names(), t As Date
Const MAX_WAIT_SEC As Long = 10
Set bot = New ChromeDriver
With bot
.get "https://trello.com/login"
.Window.Maximize
.FindElementById("user").SendKeys "userName"
.FindElementById("password").SendKeys "passWord"
.FindElementById("login").Click
.FindElementByLinkText("Gestão de Pessoas", timeout:=5000).Click '< named argument
t = Timer
Do
On Error Resume Next
Set titles = bot.FindElementsByCss(".list-header-name")
numberOfWebElements = titles.Count
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While numberOfWebElements = 0
If numberOfWebElements = 0 Then Exit Sub
ReDim names(1 To numberOfWebElements)
For i = 1 To titles.Count
names(i) = titles.item(i).Text
Next
Stop 'delete me later
End With
End Sub
回答2:
findElementsByClassName
is definitely what you want but your usage doesn't look right. I am pretty sure .count
returns a long, not a web element or an array of web elements. I am also pretty sure it doesn't take a second parameter, what are you trying to accomplish with , 5
and what was your error message?
To get the count of elements in that class
Dim classCount as Long
classCount = bot.findElementsByClassName("board-canvas").Count
Referring to the last element in that class
bot.findElementsByClassName("board-canvas").Item(classCount)
The contents of the last element in that class
bot.findElementsByClassName("board-canvas").Item(classCount).Text
Now, unfortunately, this is where I may stray a little off course because I don't have my work available for reference, but I believe you can retrieve the collection and loop through the class like this
Dim classTxt as String
Set a = bot.findElementsByClassName("board-canvas")
For each b in a
classTxt = b.Text
Next b
回答3:
I got the xpath from a random element in the class "js-list list-wrapper" and it was: //*[@id="board"]/div[5].
So i solved it using
Dim titles As Object
Set titles =
bot.findElementsByXPath("//[@id=""board""]/div")
Dim ab As Long
ab = titles.Count
MsgBox ab
来源:https://stackoverflow.com/questions/56229812/how-to-count-the-elements-from-a-class-with-vba-selenium-in-chrome