For Each Loop theough DIVs FindByClass in VBA

1,916 views
Skip to first unread message

Craig Lomas

unread,
Apr 10, 2015, 3:43:06 PM4/10/15
to seleniu...@googlegroups.com
Hi all,

I'm trying to put together a screen scraping application to collect a list of jobs from a jobs website as part of my friends PHD research.

The source code that I am trying to loop through is here:

<div class="resultsContent panel">

<div class="vacancy">

Some code....

 <h2>

    <a href="/xi/vacancy/ab57a7a2348d39dc64152c8278797dde/?vac_ref=913696760" title="Operational Manager">Operational Manager</a>
</h2>

Some more code ....

</div>

 <div class="vacancy">...</div>
 
<div class="vacancy">...</div>
 
<div class="vacancy">...</div>
 
<div class="vacancy">...</div>
 
<div class="vacancy">...</div>

I need to loop through the divs with the class "Vacancy" and then click the <a> tag shown in the code I'll then need to get some data of the new page and return to this list to continue the loop.

So far I have got the following VBA code:


Function Scrape()

Dim eRow As Long
Dim ele As Object
Dim eleGroup As Object
Dim browser As SeleniumWrapper.WebDriver
Set browser = New SeleniumWrapper.WebDriver

On Error GoTo err_scrape

myjobtype
= "manager"
mylocation
= "england"

With browser
   
.start "chrome", "http://www.jobs.nhs.uk/"
   
.Open "/"
   
'input search criteria
    .Type "keyword", myjobtype
    .Type "location", mylocation
    '
click search
   
.Click ("class=searchSubmit")
   
'refine criteria
    .Click ("id=pay_band6")
    .Click ("id=pay_band7")
    .Click ("id=pay_band8")
    .Click ("id=pay_band9")
    .Click ("id=pay_band10")
    .Click ("id=pay_band11")
    .Click ("id=pay_band12")
    .Click ("name=apply")
    '
show 100 results per page
   
.Click ("link=100")
   
    eRow
= 0
   
   
Set ele = .findElementByClassName("vacancy").findElementByTagName("div")
   
Set eleGroup = .findElementByClassName("resultsContent.panel").findElementByTagName("div")
       
   
For Each ele In eleGroup
       
Select Case ele.ClassName
           
Case "vacancy"
               
.findElementByPartialLinkText("/xi/vacancy/").Click
       
End Select
        eRow
= eRow + 1
   
Next ele
   
End With

'Macro1

Set objIE = Nothing

Exit Function

err_scrape:
MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description
Debug.Print Err.Number & " : " & Err.Description
Exit Function


End Function

So its the for each loop where I'm getting stuck .. Im currently getting the runtime error:

Element not found. Method=classname[contains], value=resultscontent.panel

Any help on the syntax I should be using or where i'm going wrong here??

Cheers

Craig

Rishikesh Ray

unread,
Jul 24, 2020, 2:59:35 PM7/24/20
to Selenium Users
Hi Criag,

Hope my response finds you well. 

You might have done with your research. Today I came across your post, while trying to do something similar. I couldn't much help on net, so had to do trial and error. I now have solution, so thought of responding to your post.

Your approach is fine, need only 2 changes.

ele should be declared as webElement, and
eleGroups as WebElements (with 's')

with this change all would work fine.

Regards,
Rishi
Reply all
Reply to author
Forward
0 new messages