硒/现场使用分页?

Selenium / Use pagination on site?

我想在这个网站上触发分页: 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 转到下一页?

进入下一页可以点击下一页元素诱导WebDriverWait for the and you can use either of the following :

  • 使用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