【问题标题】:How to interact with a non-select dropdown with selenium python如何使用 selenium python 与非选择下拉列表交互
【发布时间】:2022-01-26 17:20:41
【问题描述】:

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

可通过本网站 访问下拉菜单,然后点击“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()

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

【问题讨论】:

    标签: python selenium xpath dropdown webdriverwait


    【解决方案1】:

    在文本为 ALL SETS 的元素上click() 然后点击文本为 SET 1 的元素>你需要为element_to_be_clickable()诱导WebDriverWait而不是presence_of_element_located(),你可以使用以下Locator Strategies

    代码块:

    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()
    

    浏览器快照:

    【讨论】:

    • 非常感谢您的回答。它运作良好,但我收到了一些消息错误。我归因于代码点击太快。我在开始点击之前添加了 time.sleep(5),它每次都有效。
    猜你喜欢
    • 2022-07-21
    • 2022-01-20
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多