Home > Mobile >  Using selenium to click a dropdown menu within a secondary menu
Using selenium to click a dropdown menu within a secondary menu

Time:08-24

I am using selenium to login to a website and load a dropdown menu. Inside this menu there is a secondary menu that has a link to download a csv file. Using the following code, I can do all of those steps except click the option within the secondary menu:

driver.get(url)
driver.find_element(By.ID,"user_email").send_keys(user)
driver.find_element(By.ID,"user_password").send_keys(password)
driver.find_element(By.ID,"submit").click()
sleep(5)

driver.find_element(By.ID, "table-actions").click()
open_win_elem = driver.find_element(By.CLASS_NAME,"table-actions-menu-parent").click()
sleep(2)
driver.find_element(By.CLASS_NAME, "table-actions-menu-sub-option table-actions-option").click()  #this is where the last click doesnt happen

When I inspect the dropdown menu item that I wish to click, it looks like this:

Download / Copy PlayerList
        <div >
            <div >
                <label><input type="radio" name="players-to-include" value="all" checked="">All Players</label>
                <label><input type="radio" name="players-to-include" value="filtered">Filtered</label>
            </div>
            <label ><input type="checkbox" name="include-grouped-columns-header" checked="">Include Grouped Columns Header</label>
        </div>
        
        <div  data-action="downloadPlayerlist">Download CSV Format<i ></i></div>
        <div  data-action="downloadPlayerlistXLSX">Download XLSX Format<i ></i></div>
        <div  data-action="copyPlayerlist">Copy to Clipboard<i ></i></div>
    </div>
</div>

This returns the following error:

File "\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".table-actions-menu-sub-option table-actions-option"}

Is there something else required to similate this particular click?

CodePudding user response:

It looks like By.CLASS_NAME only takes one class name as an argument. https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name

Perhaps try using By.CSS_SELECTOR and making the argument a CSS selector that looks for both classes.

driver.find_element(By.CSS_SELECTOR, ".table-actions-menu-sub-option.table-actions-option").click()
  • Related