簡體   English   中英

Python Selenium:點擊下一頁無效

[英]Python Selenium: Clicking on the next page not working

我正在使用以下代碼通過單擊下一個元素來單擊下一頁。

但是此代碼不起作用。 關於我可能做錯了什么的任何想法。

最終目標:

在每個頁面上使用 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()

此代碼不會單擊下一頁。

Select 您的元素更具體並等待.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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM