Kyle Kyle - 3 years ago 63
Python Question

PYTHON Selenium: Click on a specific Dropdown containing Java

I am having trouble selecting from a drop down list (two actually, Season and Date). I can obtain the values which I want to input to the drop down selector, however am unable to understand what I need to do to be able to programmatically select from the dropdown.

This is the website which is of interest:
https://dataride.uci.ch/iframe/RankingDetails/1?disciplineId=10&groupId=1&momentId=19264&disciplineSeasonId=19&rankingTypeId=1&categoryId=22&raceTypeId=0

Here is the base code that I am using: (very sorry about the formatting, had trouble with long strings.)

`import requests
from selenium import webdriver
from time import sleep
from bs4 import BeautifulSoup

url='https://dataride.uci.ch/iframe/RankingDetails/1?disciplineId=10&groupId=1&momentId=19264&disciplineSeasonId=19&rankingTypeId=1&categoryId=22&raceTypeId=0' browser=webdriver.Chrome(executable_path='F:\Anaconda\chromedriver\chromedriver_win32\chromedriver.exe')

browser.get(url) season_list=browser.find_element_by_id('seasons_listbox').get_attribute('textContent')dates_list=browser.find_element_by_id('dates_listbox').get_attribute('textContent').split('Ranking')[1]

for i in range(0,len(season_list),4):
year=season_list[i:i+4]
for j in range(0,len(dates_list),10):
date=dates_list[j:j+10]
print('YEAR: ',season_list[i:i+4],' DATE ',dates_list[j:j+10])`


Here is a screenshot of the two menus I would like to be able to iterate through:
enter image description here

Any help with this would be greatly appreciated!

Answer Source

Here is the working code. I get the drop down menu using xpath and then use send_keys

from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://dataride.uci.ch/iframe/RankingDetails/1?disciplineId=10&groupId=1&momentId=19264&disciplineSeasonId=19&rankingTypeId=1&categoryId=22&raceTypeId=0'

driver.get(url)
xpath_season = '//*[@id="ranking-details-view"]/div[1]/div/div/div[1]/ul/li[2]/span'
season = driver.find_element_by_xpath(xpath_season)

xpath_date = '//*[@id="ranking-details-view"]/div[1]/div/div/div[1]/ul/li[3]/span'
date = driver.find_element_by_xpath(xpath_date)

season.send_keys('2016')
date.send_keys('31/12/2015')

If you want to select the option from the drop down list then you need to get the whole list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download