Python Selenium create loop to click through links on a page and press button on each new page Python Selenium create loop to click through links on a page and press button on each new page selenium selenium

Python Selenium create loop to click through links on a page and press button on each new page


It's been long time but just posting the answer, if someone still checking for the same kind issue some point.

from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.keys import Keys# need the below imports to work with Explicit waitfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Bybrowser = webdriver.Firefox()browser.get('thewebpage')search = browser.find_element_by_id('getSearch')search.click()search.send_keys('searchitem' + Keys.RETURN)searchitem = browser.find_elements_by_class_name("name")[0]searchitem.click()# Here is the logic that we have to update# Get number of users rather than the users.userElems = len(browser.find_elements_by_link_text("#/user/"))# iterate through each user by using the index  # if you try to use the find_elements as shown in OP, you will get StaleElement Exception  # because the user elements references will be refreshed when navigated to next page and  # load back (so we have to find the elements based on index on the page every time)for userNum in range(1,userElems):    # this below explicit wait will make sure the script will wait max 30 sec for the next user to be clicked    user = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"(#/user/)[" + str(userNum) + "]")))    # scroll user into view    user.location_once_scrolled_into_view    # click on user    user.click()    # click on follow link    follow = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"followAction")))    follow.click()    # click on browser back button    browser.back()