簡體   English   中英

使用Selenium和Python從下拉菜單中選擇

[英]Select from dropdown using Selenium and Python

SeleniumPython的幫助下。 我想抓取一個具有嵌套下拉菜單的網頁。 我只在下面發布嵌套部分:

<div class="dropDown active" data-dropdown-block="FOOTBALL_COMPSEASON" data-dropdown-default="All Seasons">
    <div class="label" id="dd-FOOTBALL_COMPSEASON">Filter by Season</div> 
    <div class="current" data-dropdown-current="FOOTBALL_COMPSEASON" role="button" tabindex="0" aria-expanded="false" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
        2018/19
    </div>
    <ul class="dropdownList" data-dropdown-list="FOOTBALL_COMPSEASON" role="listbox" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">
        <li role="option" tabindex="0" data-option-name="All Seasons" data-option-id="-1" data-option-index="-1">
             All Seasons
        </li> 
        <li role="option" tabindex="0" data-option-name="2018/19" data-option-id="210" data-option-index="0">
            2018/19
         </li>
         <li role="option" tabindex="0" data-option-name="2017/18" data-option-id="79" data-option-index="1">
              2017/18
         </li>
         <li role="option" tabindex="0" data-option-name="2016/17" data-option-id="54" data-option-index="2">
             2016/17
         </li>
    </ul>
</div>

這是它的外觀截圖:

因此,我想使搜尋器單擊下拉列表並選擇2017/18。

我首先嘗試了這個:

driver.get(_url)
select_element = driver.find_elements_by_class_name("dropdownList")[1]

由於類dropdownList在HTML中多次使用,而我所需的元素位於第二個位置,即<ul class="dropdownList"....是第二次使用類dropdown ,因此我使用了[1]得到第二個孩子。

但是然后我得到這個錯誤:

在shots_2017_18中的文件“ shots_2017_18.py”,第15行,select_element = driver.find_elements_by_class_name(“ dropdownList”) 1 IndexError:列表索引超出范圍

我應該更改或執行哪些操作,以便爬網程序可以從下拉列表中選擇2017/18項目並進行爬網?

如果您能夠使用pythonselenium單擊下拉列表。 然后,您可以嘗試以下代碼:

更新:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains
import time


driver   = webdriver.Chrome(executable_path = r'C:/Users/user***/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()

wait = WebDriverWait(driver,40)

driver.get("https://www.premierleague.com/stats/top/players/goals")  

wait.until(EC.visibility_of_element_located((By.ID, 'dd-FOOTBALL_COMPSEASON')))

time.sleep(5)
drop_down_click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.current[data-dropdown-current='FOOTBALL_COMPSEASON']")))
drop_down_click.click()

options = driver.find_elements_by_css_selector("ul[data-dropdown-list='FOOTBALL_COMPSEASON'] li")

for option in options:
  if "2017/18" in option.text.strip():
    option.click()  

UPDATE1:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains
import time

driver   = webdriver.Chrome(executable_path = r'C:/Users/user***/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()

wait = WebDriverWait(driver,40)

driver.get("https://www.premierleague.com/stats/top/players/total_scoring_att")


cookie_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.btn-primary.cookies-notice-accept")))
ActionChains(driver).move_to_element(cookie_button)
driver.execute_script('arguments[0].click();', cookie_button)
wait.until(EC.visibility_of_element_located((By.ID, 'dd-FOOTBALL_COMPSEASON')))

time.sleep(5)
drop_down_click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.current[data-dropdown-current='FOOTBALL_COMPSEASON']")))
drop_down_click.click()

options = driver.find_elements_by_css_selector("ul[data-dropdown-list='FOOTBALL_COMPSEASON'] li")

for option in options:
  if "2017/18" in option.text.strip():
    option.click()  

說明

顯式等待是您定義的代碼,用於等待特定條件發生后再繼續執行代碼。 最糟糕的情況是Thread.sleep(),它將條件設置為要等待的確切時間段。 提供了一些方便的方法,可以幫助您編寫僅等待所需時間的代碼。 WebDriverWait與ExpectedCondition結合是實現此目的的一種方法。

關於顯式等待的更多信息,可以在這里找到

當您在這種情況下超出索引范圍時,這意味着找到的元素為“無”或“僅一個”,因為您編寫的代碼正確,我認為您輸入了錯誤的URL。 但是如果URL正確,則可以使用XPATH查找適當的元素。 試試這個代碼:

select_element = driver.find_element_by_xpath("//li[@data-option-name='2017/18']")

暫無
暫無

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

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