-1

I'm trying to click on the "next page" button using selenium, but I'm having no success. Am I using the right CSS selector or should I change it to something else?

from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--enable-javascript")
options.add_argument('--no-sandbox')
options.add_argument("window-size=1200x600")

driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)

driver.get(f'https://www.stakingrewards.com/cryptoassets')
driver.implicitly_wait(10)

button = driver.find_element(By.CSS_SELECTOR,"button[data-testid='next-page-button']")

button.click()

driver.quit()
Rex G
  • 11
  • 2

1 Answers1

0

change

button = driver.find_element(By.CSS_SELECTOR,"button[data-testid='next-page-button']")

to

button = driver.find_element(By.XPATH, "//button[@data-testid='next-page-button']")

You were previously trying to select with an XPATH, but had it as CSS selector. It also needed relative pathing/corrections. Let me know if there's anything else I can help with or missed.

ljlozano
  • 181
  • 10
  • Hi @ljlozano, thanks for the response. Your answer doesn't seem to work. I get the following error ' selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages – Rex G Sep 17 '21 at 18:39
  • That sounds like your ChromeDriver or Chrome is outdated / not in-sync with each other. It's likely the usage of WebDriverManager and your version of Chrome. Please visit [here](https://stackoverflow.com/questions/21001652/chrome-driver-error-using-selenium-unable-to-discover-open-pages) for more information. I was able to replicate your code on my own machine with my example above and it works perfectly fine. – ljlozano Sep 17 '21 at 18:50
  • That shouldn't be the case because I am using webdriver manager (driver = webdriver.Chrome(ChromeDriverManager().install()) – Rex G Sep 17 '21 at 19:07
  • If you have selenium installed locally (not using WebDriverManager), and correctly setup your executable via PATH then this should work (as it does on mine with latest drivers/versions) and do what you want: `from selenium import webdriver` `d = webdriver.Chrome()` `d.get(f'https://www.stakingrewards.com/cryptoassets')` `d.find_element_by_xpath("//button[@data-testid='next-page-button']").click()` If it fails, then it's an issue with your driver/Chrome compatibility. Apologies for the formatting in comments, I'm still getting used to them. – ljlozano Sep 17 '21 at 19:29