try the following code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
import time
count = 0
browser.get('https://www.autocodes.com/obd-code-list/powertrain/1')
while(1):
try:
linkElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@id='pag']//a[contains(text(),'Next')]")))
except:
import traceback
traceback.print_exc()
print "last page reached."
break;
# type(linkElem)
count += 1
print "count " , count
linkElem.click() # follows the "Next" link
time.sleep(1)
the code continuously checks whether the Next
element is present in an infinite while loop
. If present, clicks it. Otherwise, break the loop.
Note: count
variable is added as a debug statement to know the loop count. If not required, you can remove the code related to it.
Note: similarly, traceback
is added to print the complete trace. just prints the exception trace for your reference.
Note: break
keyword, breaks the infinite loop once it reaches the last page and comes out of while
loop and continues with the next code.
References:
- http://selenium-python.readthedocs.io/waits.html