Selenium Python:无法滚动和单击“加载更多故事”按钮

Selenium Python: Unable to scroll and click on Load more stories button

我是 Selenium 的新手,希望得到建议。我正在尝试单击 NPR 网站 (https://www.npr.org/sections/news/) 上的加载更多故事按钮。我的代码看起来不错,但是当我 运行 它永远不会加载更多的故事。我没有同时收到错误代码只是没有按预期运行。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox(executable_path='/home/myname/projects/myproject/geckodriver')
# implicit wait for 5 seconds
driver.implicitly_wait(5)
#driver.maximize_window()
driver.get('https://www.npr.org/sections/news/')

element = driver.find_element(By.XPATH, '/html/body/main/div[2]/section/div[7]/button')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)

网页npr have a infinitescrollwrap. As a demonstration to thrice on Load more stories you can use the following :

  • 代码块:

    driver.get("https://www.npr.org/sections/news/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.global-modal__dismiss"))).click()
    for i in range(3):
      driver.execute_script("arguments[0].scrollIntoView();", WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.options__load-more"))))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.options__load-more"))).click()
      print("Load more stories clicked")
    
  • 控制台输出:

    Load more stories clicked
    Load more stories clicked
    Load more stories clicked
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC