簡體   English   中英

使用 selenium 單擊二級菜單中的下拉菜單

[英]Using selenium to click a dropdown menu within a secondary menu

我正在使用 selenium 登錄網站並加載下拉菜單。 在此菜單內有一個二級菜單,其中包含下載 csv 文件的鏈接。 使用以下代碼,除了單擊二級菜單中的選項外,我可以執行所有這些步驟:

driver.get(url)
driver.find_element(By.ID,"user_email").send_keys(user)
driver.find_element(By.ID,"user_password").send_keys(password)
driver.find_element(By.ID,"submit").click()
sleep(5)

driver.find_element(By.ID, "table-actions").click()
open_win_elem = driver.find_element(By.CLASS_NAME,"table-actions-menu-parent").click()
sleep(2)
driver.find_element(By.CLASS_NAME, "table-actions-menu-sub-option table-actions-option").click()  #this is where the last click doesnt happen

當我檢查我希望單擊的下拉菜單項時,它看起來像這樣:

下載/復制播放列表
File "\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".table-actions-menu-sub-option table-actions-option"}

這將返回以下錯誤:

 File "\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".table-actions-menu-sub-option table-actions-option"}

是否還需要其他東西來模擬這個特定的點擊?

看起來By.CLASS_NAME僅將一個 class 名稱作為參數。 https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name

也許嘗試使用By.CSS_SELECTOR並將參數設置為 CSS 選擇器來查找這兩個類。

driver.find_element(By.CSS_SELECTOR, ".table-actions-menu-sub-option.table-actions-option").click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM