1

I am new to Selenium and would appreciate advice. I am trying to click on the load more stories button on NPR's website (https://www.npr.org/sections/news/). My code seems good, but when I run it more stories are never loaded. I don't get an error at the same time the code just doesn't behave as expected.

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)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
generic
  • 302
  • 1
  • 3
  • 14

1 Answers1

1

The webpage npr have a infinitescrollwrap. As a demonstration to click() thrice on Load more stories you can use the following Locator Strategies:

  • Code Block:

    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")
    
  • Console Output:

    Load more stories clicked
    Load more stories clicked
    Load more stories clicked
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks so much for this response. When I try the above code, I still get an error -- this time on the 4th line (the line with driver.execute_script()` TimeoutException: Message: Stacktrace: WebDriverError@chrome://marionette/content/error.js:181:5 NoSuchElementError@chrome://marionette/content/error.js:393:5 element.find/<@chrome://marionette/content/element.js:306:16 – generic Dec 23 '21 at 20:39
  • 1
    @generic What error does it throws? I've added the required imports. – undetected Selenium Dec 23 '21 at 20:42
  • 1
    I think I had the wrong imports and adding your imports fixed the problem. This was my error: – generic Dec 23 '21 at 20:43
  • 1
    TimeoutException: Message: Stacktrace: WebDriverError@chrome://marionette/content/error.js:181:5 NoSuchElementError@chrome://marionette/content/error.js:393:5 element.find/<@chrome://marionette/content/element.js:306:16 – generic Dec 23 '21 at 20:43
  • 1
    Actually, I am not sure what happened but I am getting that same error (above) again. – generic Dec 23 '21 at 20:50
  • 1
    I am using the exact code, no edits. – generic Dec 23 '21 at 20:51
  • 1
    @generic Can you check with Chrome browser once, code is tested with Chrome. – undetected Selenium Dec 23 '21 at 20:52
  • 1
    @generic You shouldn't see a **`NoSuchElementError`** by any means with my code as I don't have `find_element*` – undetected Selenium Dec 23 '21 at 20:54
  • 1
    Let me restart everything. I may have done something dumb. – generic Dec 23 '21 at 20:57
  • 1
    I restarted VSCode, changed to chromedriver (instead of Firefox's Gecko driver), and I get the following error on that same line (driver.execute_script ...) – generic Dec 23 '21 at 21:03
  • 1
    TimeoutException: Message: Stacktrace: #0 0x56333b40aee3 #1 0x56333aed8608 #2 0x56333af0eaa1 #3 0x56333af0ec61 #4 0x56333af41714 #5 0x56333af2c29d #6 0x56333af3f3bc #7 0x56333af2c163 #8 0x56333af01bfc #9 0x56333af02c05 #10 0x56333b43cbaa #11 0x56333b452651 #12 0x56333b43db05 #13 0x56333b453a68 #14 0x56333b43205f #15 0x56333b46e818 #16 0x56333b46e998 #17 0x56333b489eed #18 0x7f50f99f9590 – generic Dec 23 '21 at 21:03
  • 1
    I am using the exact code as above, and have not run anything else. – generic Dec 23 '21 at 21:04
  • 1
    @generic The current error stacktrace is possibly of a driver-browser mismatch, can you check the versions of both? – undetected Selenium Dec 23 '21 at 21:05
  • 1
    My chrome version is: 96.0.4664.110 (for Ubuntu) My chrome driver is the same – generic Dec 23 '21 at 21:09
  • 1
    @generic There is no `ChromeDriver v96.0.4664.110` but [ChromeDriver 96.0.4664.45](https://chromedriver.storage.googleapis.com/index.html?path=96.0.4664.45/) – undetected Selenium Dec 23 '21 at 21:11
  • 1
    So then I probably need to find Chrome version v96.0.4664.45. That's what I get for not reading the last two digits right. I'll be right back after looking for that version – generic Dec 23 '21 at 21:12
  • 1
    @generic Go ahead and keep me updated. – undetected Selenium Dec 23 '21 at 21:13
  • 1
    I'm having trouble finding Chrome version 96.0.4664.45. It's not listed here: https://www.slimjet.com/chrome/google-chrome-old-version.php, and the websites that say they offer 96.0.4664.45 seem really shady and also say it's for Android. This is where I got my Chrome driver: https://chromedriver.chromium.org/downloads . Maybe I need a different chrome driver? Thanks for your thoughts. – generic Dec 23 '21 at 21:21
  • 1
    @generic For smaller and shorter doubts feel free to visit [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room to get help and at the same time help others. – undetected Selenium Dec 23 '21 at 21:23
  • 1
    Wow a whole Selenium room -- thanks for this -- I will post my driver issue there as well, and also do some more experimentation. – generic Dec 23 '21 at 21:28