title: How do I click-iterate over a list of web elements in Selenium?
body: <p>I'd like to be able to click on several links on a page of search results. To do this, I've tried to create a list of web elements finding all of the xpaths on a search results page that correspond with links. I would then like to be able to click on those links for each xpath and return to the search results page and click on the next link in the list.</p>
  
  <p>So far I've been able to create a nested loop that scrapes all of the href tags on a page to create/find the xpath. The second level of the loop is intended to click on the link corresponding to that element in the list and then return to the search page to click on the next element in the list.</p>
  
  <pre><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
  from bs4 import BeautifulSoup
  import requests
  
  driver = webdriver.Chrome('C:\\Users\\km13\\chromedriver.exe')
  driver.get("http://www.congreso.gob.pe/pley-2016-2021")
  
  
  WebDriverWait(driver, 50).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'ventana02')))
  soup = BeautifulSoup(driver.page_source, 'lxml')
  table = soup.find('table', {'cellpadding' : '2'})
  table_items = table.find_all('a')
  for item in table_items:
      elements = driver.find_elements_by_xpath("//a[@href='" + item.get('href') + "']")
      print(elements)
      for elem in elements:
          elem.click()
          driver.back()
  </code></pre>
  
  <p>This coded will click on the first link on the search results page, go to that link and then click back to the search results page. But it does not iterate on to the next link in the element list. I'm not sure if there is something wrong with the loop I wrote?</p>