Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Selenium - How to parse the URL and click next page?

I am trying to parse the hrefs and the titles of all articles from https://www.weforum.org/agenda/archive/covid-19 but I also want to pull information on the next page.

My code can only pull the current page but is not working on click() next page.

driver.get("https://www.weforum.org/agenda/archive/covid-19")

links =[]
titles = []

while True:
    for elem in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.tout__link'))):
        links.append(elem.get_attribute('href'))
        titles.append(elem.text)
    try:
        WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".pagination__nav-text"))).click()
        WebDriverWait(driver,5).until(EC.staleness_of(elem))
    except:
        break

Can anyone help me with the issue? Thank you!

like image 896
Bangbangbang Avatar asked Mar 15 '26 15:03

Bangbangbang


1 Answers

The class name 'pagination__nav-text' is not unique. As per the design, it clicks on the first found element which is "Prev" link. so you would not see that working.

Can you try with this approach,

    driver.get("https://www.weforum.org/agenda/archive/covid-19")
wait = WebDriverWait(driver,10)

links =[]
titles = []

while True:
    for elem in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.tout__link'))):
        links.append(elem.get_attribute('href'))
        titles.append(elem.text)
    try:
        print('trying to click next')
        WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH,"//div[@class='pagination__nav-text' and contains(text(),'Next')]"))).click()
        WebDriverWait(driver,5).until(EC.staleness_of(elem))
    except:
        break


print(links)   
print(titles)
driver.quit()
like image 61
Sureshmani Kalirajan Avatar answered Mar 17 '26 09:03

Sureshmani Kalirajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!