0

I can't select values in a list. I tried using find_element_by_class_name() to open the menu but when I need to select a <li> returns that element doesn't have a function click().

Here the code:

click_menu = driver.find_element_by_class_name("periodSelector")
click_menu[1].click()

Here is the HTML that I am trying to parse:

<div data-period-selector="" data-period="periodFilter">
    <div class="periodSelectorContainer">
        <div class="btn-group periodSelector">
            <button class="flat-btn dropdown-toggle periodToggle ng-binding" data-toggle="dropdown"> 20/02/2021 - 22/03/2021 <span class="dropdown-arrow"></span> </button>
            <ul class="dropdown-menu">
                <li>
                    <a href="javascript:void(0);" class="new-financ" ng-click="selectToday()"><i></i>
                        <span class="pull-left">Hoje</span>
                        <span class="pull-right"></span>
                    </a>
                </li>
                <li>
                    <a href="javascript:void(0);" class="new-financ" ng-click="selectThisWeek()"><i>
                </li>
2
  • I don't see an element with that class. And since you only have one element use click_menu.click() . Also target the a tag instead. Commented Mar 23, 2021 at 1:09
  • @ArundeepChohan It's the third DIV in the posted HTML. Commented Mar 23, 2021 at 4:46

2 Answers 2

1

There are multiple class names you have to use a css selector.

click_menu = driver.find_element_by_css_selector("button.flat-btn.dropdown-toggle.periodToggle.ng-binding")
click_menu.click()

Clicks 1st li tag.

driver.find_element_by_xpath("ul[@class='dropdown-menu']/li[1]").click()
Sign up to request clarification or add additional context in comments.

3 Comments

"Clicks 1st li tag." Your XPath is not clicking the LI, it's clicking the A tag.
Not sure if he wants the li or the a tag in the li. I am also assuming he needs to use the button to open up. He can just remove the /a if he needs the li.
My point is that you stated that your locator clicks the LI but it is instead clicking the A. You need to fix the locator or fix the description to make them match.
0

periodSelector is a class on a DIV

<div class="btn-group periodSelector">

I'm assuming that you need to click on the BUTTON

<button class="flat-btn dropdown-toggle periodToggle ng-binding" data-toggle="dropdown">

Most of those classes seem generic (probably not unique) but I'm guessing that periodToggle might be unique given the date range. Try

driver.find_element_by_css_selector("button.periodToggle").click()

NOTE: You have an error in your code. You are using .find_element_by_class_name() (singular) but have array notation on the next line, click_menu[1]. In this case, you can just use click_menu.click(). You'd only need the array notation if you were using .find_elements_by_*() (note the plural, elements).

5 Comments

I tried your suggestion but @JeffC but return:
@CristianoAmancio Your message seems to have been cut off?
Actually, I tried your suggestion and it was able to open the dropdown menu but didn't select the option. When I posted the code here you could see that each 'li' doesn't have an id for each it and all of them have the same class. Have you has any idea to deal with this? I used click_menu[1] because I thought that I could use it as a list and chose the 'li' position.
Your question was about opening the dropdown and this code seems to have accomplished that. If you have additional questions, they should be started as a new question. Make sure in that new question you add your updated code, HTML sample, etc. and you can even reference this question if you think it would help but make sure everything required to answer the question is in the new question.
If you found this answer or any other answer useful, be sure to upvote it. If this answer solved your issue, be sure to accept it as the answer so others don't spend time trying to solve the problem when it's already been solved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.