Home > Blockchain >  Selenium / Use pagination on site?
Selenium / Use pagination on site?

Time:03-27

i want to trigger the pagination on this site: https://www.kicker.de/bundesliga/topspieler/2008-09

I found the element with this XPATH in the chrome-inspector:

driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()

Now i want to click this element to go one page further - but i get an error.

This is my code:

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)   

But i get this error:

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)

How can i go to the next page using selenium?

CodePudding user response:

The go to the next page you can click on the next page element inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.kick__pagination__button--active  a"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'kick__pagination__button--active')]//following::a[1]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related