Clicking Option Text in split dropdown using Selenium

Viewed 25

Using the selenium web driver, I am attempting to iterate through pop-up dropdown on a webpage and click each individual month name, which will trigger an automatic CSV download.

The problem I'm running into is that i need to click the text in the pop-up dropdown, but using the XPath I was using either time out or the driver says that the item is unclickable.

The button and popup dropdown looks like this:

https://postimg.cc/VSP4d5rZ

https://postimg.cc/zLw7W5Tz

The element of the webpage looks like this: `

<div id="performanceMetricsExportDropdown">
                            







<span class="a-splitdropdown-container"><label for="exportPerformanceMetrics-announce" class="a-native-dropdown"><span class="dvp-sprite avdp-dashboard-icon-csv aok-inline-block aok-align-center">&nbsp;</span>
    <span>Export Month to CSV</span></label><select name="exportPerformanceMetrics" id="exportPerformanceMetrics" tabindex="-1" aria-hidden="true" class="a-native-splitdropdown a-spacing-top-micro">
    <option value="MostRecent-2022-9" data-a-css-class="aok-hidden">
        Export Month to CSV
    </option>
    
        <optgroup label="2022">
            <option value="Group-2022" data-a-css-class="avdp-dashboard-optgroup" data-a-html-content="<strong>2022</strong>">
            </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-3">
                    March
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-4">
                    April
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-5">
                    May
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-6">
                    June
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-7">
                    July
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-8">
                    August
                </option>
            
                <option data-reftag="avd_da_pm_csv" value="2022-9">
                    September
                </option>
            
        </optgroup>
    
</select><span tabindex="-1" data-a-class="aok-float-right" class="a-button-group a-button-group-splitdropdown a-spacing-top-micro aok-float-right"><span class="a-button a-button-group-first a-button-small" id="a-autoid-6"><span class="a-button-inner"><button class="a-button-text a-text-left a-declarative" data-csa-c-func-deps="aui-da-a-splitdropdown-main" data-csa-c-type="widget" data-csa-interaction-events="click" data-action="a-splitdropdown-main" type="button" id="a-autoid-6-announce"><span class="a-dropdown-label"><span class="dvp-sprite avdp-dashboard-icon-csv aok-inline-block aok-align-center">&nbsp;</span>
    <span>Export Month to CSV</span></span><span class="a-dropdown-prompt"></span></button></span></span><span class="a-button a-button-group-last a-button-splitdropdown a-button-small" id="a-autoid-7"><span class="a-button-inner"><button id="exportPerformanceMetrics-announce" class="a-button-text a-declarative" data-csa-c-func-deps="aui-da-a-splitdropdown-button" data-csa-c-type="widget" data-csa-interaction-events="click" data-action="a-splitdropdown-button" type="button" role="combobox"><i class="a-icon a-icon-dropdown"></i></button></span></span></span></span>

                        </div>

`

I attempt to click the dropdown button:
#click button driver.find_element(By.XPATH,"//*[@id='a-autoid-7']/span").click()

Then I attempt to click the report:

#clicking reports
reports_xpath = "//*[contains(@id,'exportPerformanceMetrics')]"
reports = driver.find_elements(By.XPATH,reports_xpath)

for r in range(0,len(reports)):
        try:
            WebDriverWait(driver,10).until(EC.element_to_be_clickable( (By.XPATH,reports_xpath) )).click()     
        finally:
            driver.quit() 
 
0 Answers
Related