Vba selenium code to get data from Li class

2019-08-15 08:11发布

问题:

I am trying to get data from li having same Class below is the code which i have tried but did not received any output.

Please correct me where i am wrong.

My VBA code:

Sub Class_Initialize()
Set driver = CreateObject("Selenium.FirefoxDriver")
driver.get "url"
driver.Window.Maximize
Dim dd As Variant
dd = driver.findElementsByClass("oas_columns ng-binding")(2).Text
Range("A" & Rows.Count).End(xlUp).Offset(1) = dd

'In column A i require output Abhay 28566207

dd = driver.findElementsByClass("oas_columns ng-binding")(3).Text
Range("B" & Rows.Count).End(xlUp).Offset(1) = dd

'In column B i require output Brick Field Shelters

dd = driver.findElementsByClass("oas_columns ng-binding")(4).Text
Range("C" & Rows.Count).End(xlUp).Offset(1) = dd

'In column C i require output 2018-02-12

End Sub

My HTML code:

<ul class="oas_columnsWrapper">
<li class="oas_columns ng-binding" ng-show="vm.data">1.</li>
<li class="oas_columns ng-binding">Abhay 28566207</li>
<li class="oas_columns ng-binding">Brick Field Shelters</li>
<li class="oas_columns ng-binding">2018-02-12</li>
<li class="oas_columns ng-binding">3,868</li>
<li class="oas_columns ng-binding">33</li>
<li class="oas_columns ng-binding">0.85%</li>
<li class="oas_columns semiBold active">ACTIVE</li>
<div class="oas_columns" ng-show="x['responseCount']"><a href="/do/my99acres/homepage/route/all_responses/ITOA-8157" ng-click="trackGA('Manage Listings', 'OnClick', 'ML_ShowResponse', '');" ui-sref="root.response({filter:'ITOA' + '-' + 8157})" ui-sref-opts="{reload:true}" class="ng-binding">4 Responses</a></div><li class="oas_columns oas_viewReportBtn" id="oas_viewReportBtn0" ng-click="vm.viewReport($index,$event);">View Report</li></ul>

回答1:

To get the text from the <li> tags you can use the following Locator Strategies :

  • Abhay 28566207 :

    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[2]").Text
    //or
    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[2]").Attribute("innerHTML") 
    
  • Brick Field Shelters :

    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[3]").Text
    //or
    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[3]").Attribute("innerHTML")
    
  • 2018-02-12 :

    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[4]".Text
    //or
    dd = driver.FindElementByXPath("//ul[@class='oas_columnsWrapper']//following::li[4]".Attribute("innerHTML")