sefatheking
sefatheking

Reputation: 47

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")>

Upvotes: 1

Views: 255

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193308

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
    

Upvotes: 1

Shawn
Shawn

Reputation: 8508

Use JavaScript's execute_script method to perform the click() action.

Change your code as below:

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

Full working code:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.binance.com/en/futures-activity/leaderboard/futures')
driver.maximize_window()
driver.find_element(By.ID, 'onetrust-accept-btn-handler').click()
a = driver.find_element(By.XPATH, '//*[@id="page-2"]')
driver.execute_script("arguments[0].click();", a)

Result:

enter image description here

Upvotes: 1

Related Questions