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..
 
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
Hi, thanks for spending time to share this information with me. I would definitely look into what you had provided me. In fact, I saw some code using JavascriptExecutor. I guess this is from another kind of program code. But I would like to know what kind of software the program is for?
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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