我想在這個網站上觸發分頁: https ://www.kicker.de/bundesliga/topspieler/2008-09
我在 chrome-inspector 中找到了這個 XPATH 的元素:
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
現在我想單擊此元素以進一步瀏覽一頁 - 但我收到錯誤訊息。
這是我的代碼:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from sys import platform
import os, sys
from datetime import datetime, timedelta
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent
if __name__ == '__main__':
print(f"Checking chromedriver...")
os.environ['WDM_LOG_LEVEL'] = '0'
ua = UserAgent()
userAgent = ua.random
options = Options()
options.add_argument('--headless')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_argument("--disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument(f'user-agent={userAgent}')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWebDriver = WebDriverWait (driver, 10)
seasonList = ["2008-09","2009-10","2010-11","2011-12","2012-13","2013-14","2014-15",
"2015-16","2016-17","2017-18","2018-19","2020-21", "2021-22"]
for season in seasonList:
tmpSeason = f"{season[:4]}/20{season[5:]}"
link = f"https://www.kicker.de/bundesliga/topspieler/{season}"
print(f"Working for link {link}...")
driver.get (link)
time.sleep(WAIT)
while True:
soup = BeautifulSoup (driver.page_source, 'html.parser')
tmpTABLE = soup.find("table")
tmpTR = tmpTABLE.find_all("tr")
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
time.sleep(WAIT)
但我得到這個錯誤:
Traceback (most recent call last):
File "C:\Users\Polzi\Documents\DEV\Fiverr\ORDER\fireworkenter\collGrades.py", line 116, in <module>
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webelement.py", line 693, in _execute
return self._parent.execute(command, params)
File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 400, in execute
self.error_handler.check_response(response)
File "C:\Users\Polzi\Documents\DEV\.venv\NormalScraping\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 236, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: headless chrome=99.0.4844.82)
如何使用 selenium 轉到下一頁?
uj5u.com熱心網友回復:
轉到下一頁,您可以單擊下一頁元素,為element_to_be_clickable()誘導WebDriverWait,您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.kick__pagination__button--active a"))).click()使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'kick__pagination__button--active')]//following::a[1]"))).click()注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451771.html
上一篇:使用python進行Web抓取-具有多個tbody元素的表
下一篇:OSError:[Errno8]Execformaterror:'/home/ec2-user/Desktop/chromedriver'在AWSEC2ARM風味機器中使用Chro
