简体   繁体   English

如何使用 selenium python 与非选择下拉列表交互

[英]How to interact with a non-select dropdown with selenium python

I am currently learning the use of selenium with python, and tried to collect some data.我目前正在学习selenium与python的使用,并尝试收集一些数据。 I have been struggling for the last couple days with clicking on a dropdown not accessible by Select method.在过去的几天里,我一直在努力点击 Select 方法无法访问的下拉菜单。 I looked at A LOT of questions on SOF, blogs, tutorials... and could not find the answer to my problem.我看了很多关于 SOF、博客、教程的问题……但找不到我的问题的答案。

The dropdown is accessible to this website <"https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/">, then by clicking on the "Box Score" tab.此网站可访问下拉列表 <"https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/">,然后单击“Box Score”选项卡。 Just below the teams flags, you'll see the dropdown with "ALL SETS" writing in it.在球队旗帜下方,您会看到写有“ALL SETS”的下拉菜单。

I would like to access the data from "SET 1", "SET 2", "SET 3".我想从“SET 1”、“SET 2”、“SET 3”访问数据。 My guess would be to click on the dropdown, then click on "SET 1" and so on.我的猜测是点击下拉菜单,然后点击“SET 1”等等。 But I couldn't make the code work to click on the dropdown.但是我无法使代码工作以单击下拉列表。

Below is my code:下面是我的代码:

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

Many thanks for your time and answer !非常感谢您的时间和回答!

To click() on the element with text as ALL SETS and then to click on the element with text as SET 1 instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies :click()以文本为ALL SETS的元素,然后单击以文本为SET 1而不是 present_of_element_located( )的元素,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下定位器策略

Code Block:代码块:

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

Browser Snapshot:浏览器快照:

SET1

How can I use selenium in python to select an item in a dropdown when the options are non-interactable<div> 盒子?</div><div id="text_translate"><p> 我正在尝试使用 selenium 制作一个通过臭名昭著的<a href="https://userinyerface.com/" rel="nofollow noreferrer">https://userinyerface.com/</a>运行的程序。 但是,我卡在第二页上,那里有一个下拉菜单,要求您访问顶级域 select。 下拉菜单完全由 div 和 css 组成,这意味着没有一个选项具有唯一 ID,并且似乎无法与 using.click() 进行交互:</p><pre> selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable</pre><p> 这是下拉列表的整个 HTML: <a href="https://i.stack.imgur.com/cF1wk.png" rel="nofollow noreferrer">HTML 代码</a></p><p>我实际上能做什么? 我也阅读了有关 Select class 的信息,但我认为它会产生相同的结果,并且这些元素不能通过 ID 唯一识别,因此不确定它是否可以使用任何一种方式。</p></div> - How can I use selenium in python to select an item in a dropdown when the options are non-interactable <div> boxes?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Selenium Python 的非选择下拉列表选择 - Non-Select Drop-down list selection using Selenium Python Python / Selenium 如何与隐藏的 Select 元素交互 - Python / Selenium How to interact with Hidden Select element 如何使用 Selenium 和 Python 从 GoogleForm 非选择下拉列表中选择一个选项 - How to select an option from the GoogleForm non select dropdown using Selenium and Python Selenium Python 中的 Select 下拉菜单 - Select dropdown in Selenium Python How can I use selenium in python to select an item in a dropdown when the options are non-interactable<div> 盒子?</div><div id="text_translate"><p> 我正在尝试使用 selenium 制作一个通过臭名昭著的<a href="https://userinyerface.com/" rel="nofollow noreferrer">https://userinyerface.com/</a>运行的程序。 但是,我卡在第二页上,那里有一个下拉菜单,要求您访问顶级域 select。 下拉菜单完全由 div 和 css 组成,这意味着没有一个选项具有唯一 ID,并且似乎无法与 using.click() 进行交互:</p><pre> selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable</pre><p> 这是下拉列表的整个 HTML: <a href="https://i.stack.imgur.com/cF1wk.png" rel="nofollow noreferrer">HTML 代码</a></p><p>我实际上能做什么? 我也阅读了有关 Select class 的信息,但我认为它会产生相同的结果,并且这些元素不能通过 ID 唯一识别,因此不确定它是否可以使用任何一种方式。</p></div> - How can I use selenium in python to select an item in a dropdown when the options are non-interactable <div> boxes? 如何使用Selenium在python中选择下拉菜单 - How to select dropdown menu in python using Selenium Selenium Python:如何从下拉菜单中选择项目 - Selenium Python: How to select item from a dropdown 如何在datepicker的下拉菜单中选择时间(Selenium和Python) - How to select time in dropdown of datepicker (Selenium & Python) Python Selenium:如何 select 下拉菜单 - Python Selenium: How to select dropdown menu 如何使用 Selenium Python 选择下拉菜单 - How to select a dropdown menu using Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM