PYTHON Selenium 点击包含 J 的特定下拉菜单

2024-07-02 14:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难从下拉列表中选择(实际上有两个,季节和日期)。我可以获得我想输入到下拉选择器的值,但是我无法理解我需要做什么才能从下拉列表中编程选择

这是一个有趣的网站: https://dataride.uci.ch/iframe/RankingDetails/1?disciplineId=10&groupId=1&momentId=19264&disciplineSeasonId=19&rankingTypeId=1&categoryId=22&raceTypeId=0

下面是我正在使用的基本代码:(非常抱歉格式化,长字符串有问题。)

`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])`

下面是我希望能够反复浏览的两个菜单的屏幕截图: enter image description here


Tags: fromhttpsimportbrowser列表getchchromedriver
1条回答
网友
1楼 · 发布于 2024-07-02 14:27:58

这是工作代码。我使用xpath获得下拉菜单,然后使用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')

如果您想从下拉列表中选择option,那么您需要获得整个列表

相关问题 更多 >