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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.