Python Selenium: Clicking on the next page not working

Question:

I am using the following code to click on the next page, by clicking the next element.

However this code is not working. Any thoughts on what I might be doing wrong.

Final goal:

Use BeautifulSoup on each of the pages.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as ureq
import random
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By



chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)

# A randomizer for the delay
seconds = 1 + (random.random() * 2)
# create a new Chrome session
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(30)
# driver.maximize_window()

# navigate to the application home page
driver.get("https://www.fda.gov/inspections-compliance-enforcement-and-criminal-investigations/compliance-actions-and-activities/warning-letters")
time.sleep(seconds)
time.sleep(seconds)

next_page = driver.find_element(By.CLASS_NAME, "next")
#print (next_page.get_attribute('innerHTML'), type(next_page))
next_page.find_element(By.XPATH("//a[@href='#']")).click
# next_page.find_element(By.LINK_TEXT("Next")).click()

This code does not click on the next page.

Asked By: futurenext110

||

Answers:

Select your element more specific and wait .until(EC.element_to_be_clickable()) – This will give you the next page.

Example

from selenium import webdriver
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.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

url = 'https://www.fda.gov/inspections-compliance-enforcement-and-criminal-investigations/compliance-actions-and-activities/warning-letters'

driver.get(url)

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#datatable_next a'))).click()
Answered By: HedgeHog