如何使用 selenium python 与非 select 下拉菜单交互

How to interact with a non-select dropdown with selenium python

我目前正在通过 python 学习 selenium 的使用,并尝试收集一些数据。在过去的几天里,我一直在努力单击无法通过 Select 方法访问的下拉菜单。 我在 SOF、博客、教程...上查看了很多问题,但找不到问题的答案。

可通过此网站 <"https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/"> 访问下拉列表,然后单击“Box Score”选项卡。在团队旗帜下方,您会看到下拉菜单,其中写有“ALL SETS”。

我想访问“SET 1”、“SET 2”、“SET 3”中的数据。我的猜测是点击下拉菜单,然后点击“SET 1”等等。但是我无法使代码能够点击下拉菜单。

下面是我的代码:

PATH = "C:\Program Files\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")

#implicit wait to be sure the elements we want are loaded when we try accessing them
driver.implicitly_wait(5)
actions = ActionChains(driver)


#clicking on button
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "tab-title_boxscore")))
element.click() #mimic clicking on the clickable element
dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
    (By.LINK_TEXT, "ALL SETS"))).click()
first_set = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
    (By.LINK_TEXT, "SET 1"))).click()

非常感谢您的宝贵时间和回答!

on the element with text as ALL SETS and then to click on the element with text as SET 1 instead of you need to induce WebDriverWait for the and you can use the following

代码块:

driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_boxscore"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_all"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='All Sets']//following::li[1]/a/span"))).click()

浏览器快照: