我正在嘗試在此網站上抓取有關足球運動員的資料:https : //understat.com/team/Aston_Villa/2021
這個特定的頁面是阿斯頓維拉的頁面。問題是我只需要從“最近 5 場比賽”中抓取資料。這是我所附鏈接的網頁大約一半的下拉選單。要將其從“全部”更改為“5 場比賽”,我需要單擊下拉選單,選擇“5 場比賽”,然后單擊過濾器按鈕。
我已經嘗試選擇選項“5 個游戲”,但是如果不先單擊該元素,則該元素是不可見的,而且之后我也不知道如何單擊“過濾器”按鈕。
我試過了 select = Select(driver.find_element(By.NAME, 'team-players-n-last-matches')) select.select_by_value('5')
我試過了
driver.find_element_by_xpath("//select[@name='team-players-n-last-matches']/option[text()='5 games']").click()
但是這些都不起作用,因為 A) 元素仍然不可見,并且 B) 這些選項之后不會單擊過濾器按鈕。
總之,我需要:
A) 單擊頁面中間的“全部”下拉選單
B) 選擇“5 個游戲”選項
C) 點擊過濾器按鈕應用過濾器
感謝任何可以提前提供幫助的人!!
亞當
uj5u.com熱心網友回復:
這很棘手,因為下拉選單具有自定義樣式和系結。
這里有一種方法可以讓你得到你想要的
driver.execute_script('document.evaluate("//div[text()=\'All games\']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue?.click()')
driver.execute_script('document.querySelector(\'[rel="5"]\').click()')
driver.execute_script('document.querySelector(".fa.fa-filter").click()')
我使用“所有游戲”這個詞來識別您使用 xpath 定位的下拉串列的可點擊 div,然后使用rel屬性選擇具有 5 個游戲的串列項,然后使用.fa.fa-filter類單擊過濾器按鈕
uj5u.com熱心網友回復:
要繞過 的元素可見性要求driver.find_element,您可以改用driver.execute_script并運行自定義 Javascript:
from selenium import webdriver
d = webdriver.Chrome('/path/to/chromedriver')
d.get('https://understat.com/team/Aston_Villa/2021')
d.execute_script("""
document.querySelector('div.block:nth-of-type(4) div.filter:nth-of-type(2) .custom-select-styled').click()
document.querySelector('div.block:nth-of-type(4) div.filter:nth-of-type(2) li[rel="5"]').click()
document.querySelector('button#team-players-filter').click()
""")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369596.html
上一篇:我怎樣才能得到這個Xpath
下一篇:解釋硒如何搜索元素
