With the help of Selenium and Python. I wanted to crawl a webpage which has a nested drop-down menu. I am posting only the nested section below:
<div class="dropDown active" data-dropdown-block="FOOTBALL_COMPSEASON" data-dropdown-default="All Seasons">
<div class="label" id="dd-FOOTBALL_COMPSEASON">Filter by Season</div>
<div class="current" data-dropdown-current="FOOTBALL_COMPSEASON" role="button" tabindex="0" aria-expanded="false" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
2018/19
</div>
<ul class="dropdownList" data-dropdown-list="FOOTBALL_COMPSEASON" role="listbox" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
<li role="option" tabindex="0" data-option-name="All Seasons" data-option-id="-1" data-option-index="-1">
All Seasons
</li>
<li role="option" tabindex="0" data-option-name="2018/19" data-option-id="210" data-option-index="0">
2018/19
</li>
<li role="option" tabindex="0" data-option-name="2017/18" data-option-id="79" data-option-index="1">
2017/18
</li>
<li role="option" tabindex="0" data-option-name="2016/17" data-option-id="54" data-option-index="2">
2016/17
</li>
</ul>
</div>
Here is the screenshot of how it looks:
So, I wanted to make the crawler click the drop-down and select 2017/18.
I first tried this:
driver.get(_url)
select_element = driver.find_elements_by_class_name("dropdownList")[1]
As the class dropdownList is used multiple times in the HTML and my desired element is in the second position, i.e. <ul class="dropdownList".... is the second time the class dropdown is used, so I used [1] to get the second child.
But then I get this error:
File "shots_2017_18.py", line 15, in shots_2017_18 select_element = driver.find_elements_by_class_name("dropdownList")1 IndexError: list index out of range
What should I change or do so that the crawler can select the 2017/18 item from the dropdown list and can crawl?
