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:
JavaScript
x
2
1
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
2
Now i want to click this element to go one page further – but i get an error.
This is my code:
JavaScript
1
52
52
1
import time
2
from bs4 import BeautifulSoup
3
from selenium import webdriver
4
from selenium.webdriver.chrome.options import Options
5
from selenium.webdriver.chrome.service import Service
6
from sys import platform
7
import os, sys
8
from datetime import datetime, timedelta
9
from selenium.webdriver.support.ui import WebDriverWait
10
from selenium.webdriver.support import expected_conditions as EC
11
from selenium.webdriver.common.by import By
12
from selenium.webdriver.common.keys import Keys
13
from webdriver_manager.chrome import ChromeDriverManager
14
from fake_useragent import UserAgent
15
16
if __name__ == '__main__':
17
print(f"Checking chromedriver...")
18
os.environ['WDM_LOG_LEVEL'] = '0'
19
ua = UserAgent()
20
userAgent = ua.random
21
options = Options()
22
options.add_argument('--headless')
23
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
24
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
25
options.add_argument("--disable-infobars")
26
options.add_argument("--disable-extensions")
27
options.add_argument("start-maximized")
28
options.add_argument('window-size=1920x1080')
29
options.add_argument('--no-sandbox')
30
options.add_argument('--disable-gpu')
31
options.add_argument(f'user-agent={userAgent}')
32
srv=Service(ChromeDriverManager().install())
33
driver = webdriver.Chrome (service=srv, options=options)
34
waitWebDriver = WebDriverWait (driver, 10)
35
36
seasonList = ["2008-09","2009-10","2010-11","2011-12","2012-13","2013-14","2014-15",
37
"2015-16","2016-17","2017-18","2018-19","2020-21", "2021-22"]
38
for season in seasonList:
39
tmpSeason = f"{season[:4]}/20{season[5:]}"
40
link = f"https://www.kicker.de/bundesliga/topspieler/{season}"
41
print(f"Working for link {link}...")
42
driver.get (link)
43
time.sleep(WAIT)
44
45
while True:
46
soup = BeautifulSoup (driver.page_source, 'html.parser')
47
tmpTABLE = soup.find("table")
48
tmpTR = tmpTABLE.find_all("tr")
49
50
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
51
time.sleep(WAIT)
52
But i get this error:
JavaScript
1
14
14
1
Traceback (most recent call last):
2
File "C:UsersPolziDocumentsDEVFiverrORDERfireworkentercollGrades.py", line 116, in <module>
3
driver.find_element(By.XPATH,"//a[@class='kick__pagination__button kick__icon-Pfeil04 kick__pagination--icon']").click()
4
File "C:UsersPolziDocumentsDEV.venvNormalScrapinglibsite-packagesseleniumwebdriverremotewebelement.py", line 80, in click
5
self._execute(Command.CLICK_ELEMENT)
6
File "C:UsersPolziDocumentsDEV.venvNormalScrapinglibsite-packagesseleniumwebdriverremotewebelement.py", line 693, in _execute
7
return self._parent.execute(command, params)
8
File "C:UsersPolziDocumentsDEV.venvNormalScrapinglibsite-packagesseleniumwebdriverremotewebdriver.py", line 400, in execute
9
self.error_handler.check_response(response)
10
File "C:UsersPolziDocumentsDEV.venvNormalScrapinglibsite-packagesseleniumwebdriverremoteerrorhandler.py", line 236, in check_response
11
raise exception_class(message, screen, stacktrace)
12
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
13
(Session info: headless chrome=99.0.4844.82)
14
How can i go to the next page using selenium?
Advertisement
Answer
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:
JavaScript121WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.kick__pagination__button--active +a"))).click()
2
Using XPATH:
JavaScript121WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'kick__pagination__button--active')]//following::a[1]"))).click()
2
Note: You have to add the following imports :
JavaScript141from selenium.webdriver.support.ui import WebDriverWait
2from selenium.webdriver.common.by import By
3from selenium.webdriver.support import expected_conditions as EC
4