Search code examples
selenium-webdriverxpathclickwebdriverwaitjs-scrollintoview

How to click on the pagination element on binance.com website with Selenium Python


Can't click on found element with Selenium python. I'm trying to scrape the link:

https://www.binance.com/en/futures-activity/leaderboard/futures

I have successfully scraped the first page, however, I am trying to get to page 2. For this I use the following commands, but it is not working.

a = driver.find_element(By.XPATH,'//*[@id="page-2"]')
a.click()

Oddly, when I print(a) it finds the element, but unfortunately I can't click it. Print(a) answer:

<selenium.webdriver.remote.webelement.WebElement (session="9dbf9eafdc008e627b8b13face212b45", element="454a2787-33df-4067-b93a-8b73e259bebb")>

Solution

  • To click on Page 2 you need to scrollIntoView() the element first then inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using XPATH and id attribute:

      driver.get("https://www.binance.com/en/futures-activity/leaderboard/futures")
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@id='page-2']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='page-2']"))).click()
      
    • Using XPATH and aria-label attribute:

      driver.get("https://www.binance.com/en/futures-activity/leaderboard/futures")
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@aria-label='Page number 2']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Page number 2']"))).click()
      
    • Using XPATH and text():

      driver.get("https://www.binance.com/en/futures-activity/leaderboard/futures")
      driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@data-bn-type='button' and text()='2']"))))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-bn-type='button' and text()='2']"))).click()
      
    • Browser snapshot:

    binance

    • 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