Home » excel » excel – Unexpected Selenium behaviour after selecting from dropdown

excel – Unexpected Selenium behaviour after selecting from dropdown

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve written a Selenium script in VBA to select from a dropdown then press the Done button.

By default the page shows Bangladesh in it’s landing page. I need to change it to Canada.

My script selects the desired country from the dropdown but when it is supposed to click the Done button, it clicks elsewhere so the country remains unchanged. The script doesn’t show any error.

Site link:

Sub SelectDropdown()
    Dim driver As New ChromeDriver

    With driver
        .get "https://www.amazon.com/dp/B071V5DQ56/"
        .FindElementByCss("#nav-packard-glow-loc-icon", timeout:=5000).Click
        .FindElementByCss("#GLUXCountryListDropdown", timeout:=5000).Click
        .FindElementById("GLUXCountryList", timeout:=5000).AsSelect.SelectByText "Canada"
        .FindElementByCss("div.a-popover-wrapper button[name='glowDoneButton']", timeout:=5000).Click
    End With
    Stop
End Sub

This is what it shows in the landing page.

enter image description here

How to&Answers:

Seems like it needs a little breather before the final click.

Option Explicit
Public Sub SelectDropdown()
    Dim driver As New ChromeDriver
    With driver
        .get "https://www.amazon.com/dp/B071V5DQ56/"
        .FindElementByCss("#nav-packard-glow-loc-icon", timeout:=5000).Click
        .FindElementByCss("#GLUXCountryListDropdown", timeout:=5000).Click
        .FindElementById("GLUXCountryList", timeout:=5000).AsSelect.SelectByText "Canada"
        Application.Wait Now + TimeSerial(0, 0, 1)
        .FindElementByCss("[data-action='a-popover-close']", timeout:=5000).Click
    End With
End Sub