问题
I have been learning VBA for a project and I am having trouble grabbing certain elements out of the html and have them populate on an excel spreadsheet.
The code I have used has returned no errors and from what I can see it should be working.
Here is my VBA Code:
Option Explicit
Public Sub GrabShipping()
Dim t As Date
Dim ele As Object
Dim driver As New ChromeDriver
Dim post As WebElement
Dim i As Integer
Dim mysheet As Worksheet
Const MAX_WAIT_SEC As Long = 10
Const INURL = "https://ss3.shipstation.com/#/dashboard"
Const URL = "https://ss3.shipstation.com/"
Set mysheet = Sheets("Main")
With driver
.Start "Chrome"
.get URL
t = Timer
Do
On Error Resume Next
Set ele = .FindElementById("username")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
ele.SendKeys "Username"
.FindElementById("password").SendKeys "Password"
.FindElementById("btn-login").Click
End With
With driver
.get INURL
i = 2
For Each post In driver.FindElementsByXPath("//div[contains(@class,'row-fluid stats')]")
mysheet.Cells(i, 1) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'New Orders'").Attribute("New Orders")
mysheet.Cells(i, 2) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'Ready to Ship'").Attribute("Ready to Ship")
mysheet.Cells(i, 3) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'Orders Shipped'").Attribute("Orders Shipped")
Next post
Stop '<==delete me later
.Quit
End With
End Sub
Here is the HTML that I am trying to grab from:
<div class="header row-fluid"><div class="row-fluid stats">
<div class="col-sm-4 col-md-4 col-lg-4">
<h2>2,318</h2>
New Orders
</div>
<div class="col-sm-4 col-md-4 col-lg-4">
<h2>53</h2>
Ready to Ship
</div>
<div class="col-sm-4 col-md-4 col-lg-4">
<h2>2,265</h2>
Orders Shipped
</div>
</div></div>
I am expecting it to return the values in the s to my spreadsheet but currently when I run the code it results in nothing being added.
回答1:
You can use a css selector combination
Dim item As Object, nodeList As Object, r As Long
Set nodeList = driver.findElementsByCss(".col-sm-4.col-md-4.col-lg-4 h2")
For each item in nodeList
r = r + 1
Activesheet.Cells(r,1) = item.text
Next
You can try re-using a timed loop
Dim item As Object, nodeList As Object, r As Long
t = Timer
Do
Set nodeList = driver.FindElementsByCss(".col-sm-4.col-md-4.col-lg-4 h2")
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While nodeList.Count = 0
If nodeList.Count > 0 Then
For Each item In nodeList
r = r + 1
ActiveSheet.Cells(r, 1) = item.Text
Next
End If
I would recommend seeing if you can shorten the css selector, for example, to:
.col-sm-4 h2
回答2:
Instead of
post.FindElementByXPath(".//*[following-sibling:[contains(text(),'New Orders'").Attribute("New Orders")
Could you please try the below code and let me know if this help.
post.FindElementByXPath("//div[@class='row-fluid stats']/div/h2").Text
回答3:
You can identify <h2> node text using the below xpath :
//div[contains(@class,'row-fluid stats')]/div/h2
But you may get multiple matches as there are multiple <h2> tags for the provided class.
As you are looping, I assume that the loop will fetch the values one by one from the list so you can modify your
i = 2
For Each post In driver.FindElementsByXPath("//div[contains(@class,'row-fluid stats')]")
mysheet.Cells(i, 1) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'New Orders'").Attribute("New Orders")
mysheet.Cells(i, 2) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'Ready to Ship'").Attribute("Ready to Ship")
mysheet.Cells(i, 3) = post.FindElementByXPath(".//*[following-sibling:[contains(text(),'Orders Shipped'").Attribute("Orders Shipped")
Next post
like below :
i = 2
j = 1
For Each post In driver.FindElementsByXPath("//div[contains(@class,'row-fluid stats')]/div/h2")
mysheet.Cells(i, j) = post.FindElementByTag("h2").Text
j = j + 1
Next post
If the above code doesn't work the try the below code which will try to get that text using the class name and the tag name :
i = 2
j = 1
For Each post In driver.FindElementsByClass("row-fluid stats")
mysheet.Cells(i, j) = post.FindElementByTag("h2").Text
j = j + 1
Next post
I hope it helps...
来源:https://stackoverflow.com/questions/54653504/how-to-grab-the-h2-text-from-div-using-vba-selenium-on-chrome-and-insert-dat