[英]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:浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.