Python selenium :点击下一页不起作用

oewdyzsn  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(193)

我使用下面的代码通过单击next元素来单击下一页。
然而,这个代码不起作用。任何想法,我可能做错了什么。
最终目标:
在每一页上使用BeautifulSoup。

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

此代码不点击下一页。

ql3eal8s

ql3eal8s1#

选择你的元素更具体,并等待.until(EC.element_to_be_clickable())-这将给你下一页。

示例
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()

相关问题