1

I'm trying to go through a website one page at a time, clicking on the element next, which in the HTML code is class="pg-next". Eventually, though, I'm going to get to the end, and there won't be a next element anymore, at which point I'd like to stop the loop. I have something like this:

pg_next_exists = True
while pg_next_exists:
    #
    # carry out necessary actions
    #
    if ...: # if the pg-next element can be found
        pass
    else:
        pg_next_exists = False # at which point the while loop stops

How do I check if that element still exists?

Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • 1
    Possible duplicate of [Python - Selenium WebDriver - Checking element exists](http://stackoverflow.com/questions/9567069/python-selenium-webdriver-checking-element-exists) – Louis Oct 07 '15 at 11:11

1 Answers1

0

Why don't you use wait try something like this

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

browser = webdriver.Firefox()
browser.get('http://example.com/')

try:
     wait = WebDriverWait(browser, 10) 
     search = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'next')))
     search.click()
except:
     # Process element not found here
Prabhakaran8737
  • 81
  • 3
  • 17