Vba selenium代码从Li类获取数据

时间:2018-02-28 10:23:32

标签: vba selenium firefox

我试图从li获得具有相同类别的数据,这是我尝试但没有收到任何输出的代码。

请纠正我的错误。

我的VBA代码:

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

我的HTML代码:

<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 个答案:

答案 0 :(得分:1)

要获取<li>代码中的文字,您可以使用以下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")