VBA Selenium code for drop down menu and scroll all the way down

JasonLim

New Member
Joined
Dec 23, 2021
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi, I am looking for VBA Selenium code for the drop down menu and scroll all the way down. I had a problem selecting the option without scrolling down all the way. I tried "WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")" but it only scrolled down the main webpage but not the drop down menu. I hope there are people kind enough to help me out. Your help will be very much appreciated..
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
To answer the question we need to analyze and interact with the web page, can you share its url?
 
Upvote 0
Hi, Bro, thank you so much for your helping hand. Below is my rough script. error box appear during the selection under the condition field,

Sub Corousell_Run()
Dim WPage As Selenium.WebDriver, myUrl As String
Dim tObj As Object, myTim As Single, AColl As Object, BColl As Object, DColl As Object, FColl As Object
Dim noBB As Boolean, MI As Long
Dim NextR As Long, cCnt As Long, cCnt2 As Long
Dim NO1 As String
Dim NO2 As String
Dim LValue As String
Dim DataText As String
Dim anum As String
Dim WebsiteURLAddress As String
'
myTim = Timer

Set WPage = CreateObject("Selenium.CHRomedriver")


SearchProductItemEnd = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(-13, 0).Row

myUrl = "smart watch - View all smart watch ads in Carousell Singapore" 'Your Url
Sheets("sheet2").Select '<<< The sheets that will be used
'
WPage.Get myUrl
WPage.Wait 500
Debug.Print ">Page loaded", Format(Timer - myTim, "0.0")
'
'Search the menu container:
Set AColl = WPage.FindElementsByTag("div")
For i = 1 To AColl.Count
If InStr(1, AColl(i).Attribute("style"), "display:", vbTextCompare) = 1 Then
Debug.Print ">Found menus DIV # " & i
Exit For
End If
Next i
MI = i
'...and click the second one:
Set tObj = AColl(MI).FindElementsByTag("svg")
Debug.Print tObj.Count
tObj(1).FindElementByXPath("./..").Click
WPage.Wait 500
'Search the RadioButtons Container:
Set BColl = WPage.FindElementsByTag("div")
For i = 1 To BColl.Count
If InStr(1, BColl(i).Attribute("style"), "left:", vbTextCompare) = 1 Then
Debug.Print ">Found RadioButtons DIV # " & i
Exit For
End If
Next i
'...and click the second one:
Set tObj = BColl(i).FindElementsByTag("label")
Debug.Print tObj.Count
tObj(2).Click
WPage.Wait 500
'Debug.Print "D_FK", AColl(3).Text
Debug.Print ">ZxZx", tObj(2).Attribute("type"), tObj(2).Attribute("value")
If InStr(1, AColl(MI).Text, "Recent", vbTextCompare) > 0 Then
Debug.Print ">Recent has been selected"
noBB = False
Else
noBB = True
End If
Debug.Print ">noBB=" & noBB
If noBB Then
AppActivate (Application.Caption)
MsgBox ("Selecting Recent failed; select it manually before closing the msgbox")
''Stop '<<< This is only for test
End If
WPage.Wait 500
'Look for the Products:
Debug.Print ">Let's search the Products", Format(Timer - myTim, "0.0")
'Scroll to end of page..
'WPage.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);")
WPage.Wait 1500




i = 0




skip:

Set tObj = WPage.FindElementsByTag("Main")(1)

'For PageRun = 1 To 10


Set DColl = tObj.FindElementsByTag("button")
Debug.Print DColl.Count

x = DColl.Count
If x > 38 Then
DColl(x).Click
WPage.Wait 5000
End If


d = 0
Set BColl = WPage.FindElementsByTag("div")
For i = 1 To BColl.Count
If InStr(1, BColl(i).Attribute("style"), "display:", vbTextCompare) = 1 Then
Debug.Print ">Found RadioButtons DIV # " & i
d = d + 1

If d < 10 Then
GoTo skip3
End If

Exit For
End If
skip3:
Next i
M2 = i

Set tObj = BColl(M2).FindElementsByTag("button")

Debug.Print tObj.Count
tObj(1).Click





d = 0
Set BColl = WPage.FindElementsByTag("div")
For i = 1 To BColl.Count
If InStr(1, BColl(i).Attribute("role"), "listbox", vbTextCompare) = 1 Then
Debug.Print ">Found RadioButtons DIV # " & i
d = d + 1

If d < 1 Then
GoTo skip4
End If

Exit For
End If
skip4:
Next i
M2 = i

Set tObj = BColl(M2).FindElementsByTag("button")

Debug.Print tObj.Count
'====================================Problem Start from here, error box appear, than allow it to work, you need to manually scroll all the way down the drop down menu then you will be enabled to click and that is the problem
tObj(1).Click

d = 0
Set BColl = WPage.FindElementsByTag("div")
For i = 1 To BColl.Count
If InStr(1, BColl(i).Attribute("data-testid"), "Brand new", vbTextCompare) = 1 Then
Debug.Print ">Found RadioButtons DIV # " & i
d = d + 1

If d < 1 Then
GoTo skip5
End If

Exit For
End If
skip5:
Next i
M2 = i



Set tObj = BColl(M2).FindElementsByName("class")

Debug.Print tObj.Count
BColl(M2).Click




d = 0
Set BColl = WPage.FindElementsByTag("div")
Debug.Print BColl.Count
For i = 1 To BColl.Count
If InStr(1, BColl(i).Attribute("id"), "ReactModalPortal-SearchFiltersBarMoreRightPanelDesktop", vbTextCompare) = 1 Then
Debug.Print ">Found RadioButtons DIV # " & i
d = d + 1

If d < 2 Then
GoTo skip6
End If

Exit For
End If
skip6:
Next i
M2 = i


Set tObj = BColl(M2).FindElementsByTag("button")
Debug.Print tObj.Count
tObj(3).Click


End Sub
 
Upvote 0
I'll examine your problem as soon I find some spare time to waste reformatting the code you published without using "tags" and make it readable
 
Upvote 0
The macro select some elements here and there (and sometime, for example when the loop includes GoTo skip3, I seem that you just hope to hit the right item); at a certain point you click the button “More filters” and on the line marked “Problem Start from here” the error “element click intercepted: Element [..] is not clickable [..] Other element would receive the click” arise
Unfortunately I have no idea of what you are willing to do, and I am quite sure that “More filters” have been clicked at random; thus I have no useful suggestion to offer.

Maybe if you partition the macro and be more specific at the task you wish to perform I could come with some suggestions; maybe
 
Upvote 0
Bro, thanks for spending time to look at the issue. I am not sure you had tried to scroll all the way down the drop down menu? Yes, you did that, you will notice that the click code will be executed. If that is the problem, I am just wondering, will there be a code to do the scroll down function? I was looking around on the website, but I can't find any code like that. I seek for you if you can share with me. sorry for any inconvenience caused.
 
Upvote 0
I see...
You intentionally open the "More Filters" drop-down list and believe that the element to be clicked is simply not visible, thus not clickable?
Try adding the following lines in the shown position:
VBA Code:
Set tObj = BColl(M2).FindElementsByTag("button")
'>> ADD these lines here:
Dim kKeys As New Selenium.Keys
tObj(1).SendKeys (kKeys.End)
WPage.Wait 500
'<< End Added lines

Debug.Print tObj.Count
'====================================Problem Start from here, error box appear, than allow it to work, you need to manually scroll all the way down the drop down menu then you will be enabled to click and that is the problem
tObj(1).Click
This way the list will be scrolled to the end before tObj(1).Click
 
Upvote 0
I see...
You intentionally open the "More Filters" drop-down list and believe that the element to be clicked is simply not visible, thus not clickable?
Try adding the following lines in the shown position:
VBA Code:
Set tObj = BColl(M2).FindElementsByTag("button")
'>> ADD these lines here:
Dim kKeys As New Selenium.Keys
tObj(1).SendKeys (kKeys.End)
WPage.Wait 500
'<< End Added lines

Debug.Print tObj.Count
'====================================Problem Start from here, error box appear, than allow it to work, you need to manually scroll all the way down the drop down menu then you will be enabled to click and that is the problem
tObj(1).Click
This way the list will be scrolled to the end before tObj(1).Click
Thank you so much, it works amazing. I had learned something new from you bro.
 
Upvote 0
Both,

You may be interested in my code below. I'm still learning about Selenium Chrome/Edge automation and its differences to IE and the MS HTML library. Apart from the different classes and methods, etc. the biggest difference I've found is that Selenium works in the same way that a user would interact with a web page. For example, it has to click on a dropdown to display the options, allowing them to be selected; if an element isn't visible on the page, it has to scroll to make it visible. None of this is necessary with IE/MS HTML - if an element is present in HTML DOM, it can be automated directly.

VBA Code:
Option Explicit

Dim driver As Selenium.EdgeDriver

Public Sub Edge_Click_Dropdown_Options()

    Dim URL As String
    Dim span As WebElement
    Dim button As WebElement
    Dim selectOption As WebElement
    Dim listboxDiv As WebElement
    Dim form As WebElement
    
    URL = "https://www.carousell.sg/search/smart%20watch?addRecent=true&canChangeKeyword=true&includeSuggestions=true&searchId=rojMFM&t-search_query_source=direct_search"
        
    Set driver = New Selenium.EdgeDriver
    With driver
        .Start
        .Get URL
    End With
    
    'Find 'More filters' and click its button  
    
    Set span = driver.FindElementByXPath("//span[text()='More filters']", timeout:=1000, Raise:=False)
    Set button = span.FindElementByXPath("./../..")
    button.Click    

    'Find Condition dropdown and click its button
    
    Set listboxDiv = driver.FindElementById("FieldSetField-Container-field_layered_condition")
    Set button = listboxDiv.FindElementByXPath(".//button")
    driver.ExecuteScript "arguments[0].scrollIntoView(true);", button
    button.Click
    
    'Find and click the 'Like new' and 'Heavily used' options
    
    Set selectOption = listboxDiv.FindElementByXPath(".//*[@data-testid='Like new']")
    selectOption.Click
    
    Set selectOption = listboxDiv.FindElementByXPath(".//*[@data-testid='Heavily used']")
    selectOption.Click
    
    'Find and click the Apply button.  This button is a child of the form, which is the parent-parent of the Condition dropdown
    
    Set form = listboxDiv.FindElementByXPath("./../..")
    Set button = form.FindElementByXPath(".//button[@role='submitButton']")
    button.Click
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,125
Messages
6,123,193
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top