title: Loop through links using Selenium Webdriver (Python)
body: <p>Afternoon all. Currently trying to use Selenium webdriver to loop through a list of links on a page. Specifically, it's clicking a link, grabbing a line of text off said page to write to a file, going back, and clicking the next link in a list. The following is what I have:</p>
  
  <pre><code>    def test_text_saver(self):
      driver = self.driver
      textsave = open("textsave.txt","w")
      list_of_links = driver.find_elements_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[2]/div/div/ul/li")
      """Initializing Link Count:"""
      link_count = len(list_of_links)
      while x &lt;= link_count:
          print x
          driver.find_element_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[2]/div/div/ul/li["+str(x)+"]/a").click()
          text = driver.find_element_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[1]/div[1]/h1").text
          textsave.write(text+"\n\n")
          driver.implicitly_wait(5000)
          driver.back()
          x += 1
      textsave.close()
  </code></pre>
  
  <p>When run, it goes to the initial page, and...goes back to the main page, rather than the subpage that it's supposed to. Printing x, I can see it's incrementing three times rather than one. It also crashes after that. I've checked all my xpaths and such, and also confirmed that it's getting the correct count for the number of links in the list.</p>
  
  <p>Any input's hugely appreciated--this is really just to flex my python/automation, since I'm just getting into both.  Thanks in advance!!</p>