Home > Back-end >  Selenium - couldn't click next page
Selenium - couldn't click next page

Time:06-21

I'm trying to click the next button (>) and then repeat until the last page is reached. I've tried searching for solutions from similar questions but I couldn't figure out what's wrong/make it work.

Here is my code, thank you!

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

driver = webdriver.Chrome("C:/Users/krish/Desktop/chromedriver_win32/chromedriver.exe")

driver.get('https://www.cnbcindonesia.com/tag/pasar-modal')
wait = WebDriverWait(driver,30)


while True:
 try:
    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class = 'icon icon-angle-right']/i")))
    if (element != 0):
        element.click()
 except TimeoutException as ex:
        break

CodePudding user response:

Your xpath looks incorrect, please try the below

Xpath

//i[@class='icon icon-angle-right']

CSS Selector

.icon.icon-angle-right

CodePudding user response:

Try:

while True:
 try:
    element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@]/..')))
    if (element != 0):
        element.click()
 except TimeoutException as ex:
        break
  • Related