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

I am new to Python and Selenium, but starting to pick it up. I was looking for ways to solve this problem, but I can not find the exact solution.

What I'm trying to do is click on all the links to the username on the page, click the "Next" button on the page that I took to, then return to the original page and do the same for the rest of the link username.

Basically, I want to create a loop that does this:

  • click on the first username
    • click on the button
    • return to previous page
  • click on the second username
    • click on the button
    • return to previous page

ETC ..... through each link

Here is my current code and what I have tried so far:

from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = 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() #I am now on the page where it shows the users #this is where I'm getting stuck #here the first code I tried links = browser.find_elements_by_link_text("#/user/") for link in links: link.click() follow = browser.find_element_by_class_name("followAction") browser.back() #here the second code I tried import selenium.webdriver.support.ui as UI def test(self): driver = self.driver wait = UI.WebDriverWait(driver, 5000) links = driver.find_elements_by_link_text("#/user/") for link in links: link.click() follow = driver.find_element_by_class_name("followAction") follow.click() driver.implicityly_wait(5) driver.back() 

The program ends and nothing happens on the screen. There is no error message.

What should I change to click each link on the homepage and click the button on the linked pages?

Here is a link to a similar problem. Link scrolling through Selenium Webdriver (Python)

Your help is greatly appreciated.

+5
source share
1 answer

A lot of time passed, but I just posted the answer if someone is still checking the same type of question.

 from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.keys import Keys # need the below imports to work with Explicit wait from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By browser = 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() 
0
source

All Articles