0

How do I paginate to the next page in selenium python?

Code trials:

import os,json,requests, time
from pprint import pprint
from selenium import webdriver
import csv

driver = webdriver.Chrome()

driver.get('https://www.ebay.co.uk/b/Cordless-Drills/184655/bn_9863722?rt=nc&_dmd=1')
time.sleep(0.5)


links = driver.find_elements_by_xpath('//div[@class="s-item__wrapper clearfix"]//a[@class="s-item__link"]')
for link in links:
    print(link.get_attribute('href'))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To paginate to the next page you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.ebay.co.uk/b/Cordless-Drills/184655/bn_9863722?rt=nc&_dmd=1')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#gdpr-banner-accept[aria-label='Accept privacy terms and settings']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.ebayui-pagination__li.ebayui-pagination__li--selected +li > a"))).click()
    
  • Using XPATH:

    driver.get('https://www.ebay.co.uk/b/Cordless-Drills/184655/bn_9863722?rt=nc&_dmd=1')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='gdpr-banner-accept']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='ebayui-pagination__li ebayui-pagination__li--selected']//following::li[1]/a"))).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
    
  • Browser Snapshot:

ebay_uk_page2

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352