【问题标题】:Unexpected Selenium behaviour after selecting from dropdown从下拉列表中选择后出现意外的 Selenium 行为
【发布时间】:2025-12-07 12:10:01
【问题描述】:

我在 VBA 中编写了一个 Selenium 脚本以从下拉列表中进行选择,然后按 Done 按钮。

默认情况下,页面在其登录页面中显示Bangladesh。我需要把它改成Canada

我的脚本从下拉列表中选择了所需的国家,但是当它应该单击Done 按钮时,它会单击其他地方,因此国家/地区保持不变。该脚本没有显示任何错误。

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

这是它在登录页面中显示的内容。

【问题讨论】:

    标签: excel vba selenium selenium-webdriver web-scraping


    【解决方案1】:

    在最后一次点击之前似乎需要稍作喘息。

    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
    

    【讨论】:

    • 嘿@QHarr,感谢您的解决方案。我的问题可能误导了你。我的脚本可以从下拉列表中成功选择 Country Canada。它不能做的是按下Done 按钮来反映对该页面的更改。再一次 - 这一切都是关于 prssing Done 按钮。顺便说一句,我试过你的脚本,但还是和之前一样。
    • 没办法!!!是的,你成功了@QHarr。你知道vba selenium binding中是否有Focus的选项吗?
    • 通常使用moveToElement()
    最近更新 更多