簡體   English   中英

如何使用 selenium python 與非選擇下拉列表交互

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

我目前正在學習selenium與python的使用,並嘗試收集一些數據。 在過去的幾天里,我一直在努力點擊 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()

非常感謝您的時間和回答!

click()以文本為ALL SETS的元素,然后單擊以文本為SET 1而不是 present_of_element_located( )的元素,您需要為element_to_be_clickable()誘導WebDriverWait ,您可以使用以下定位器策略

代碼塊:

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

瀏覽器快照:

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 的非選擇下拉列表選擇 Python / Selenium 如何與隱藏的 Select 元素交互 如何使用 Selenium 和 Python 從 GoogleForm 非選擇下拉列表中選擇一個選項 Selenium Python 中的 Select 下拉菜單 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> 如何使用Selenium在python中選擇下拉菜單 Selenium Python:如何從下拉菜單中選擇項目 如何在datepicker的下拉菜單中選擇時間(Selenium和Python) Python Selenium:如何 select 下拉菜單 如何使用 Selenium Python 選擇下拉菜單
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM