title: Python and Selenium: Automatically adjusting range depending on if something exists
body: <p>The first post on Stackoverflow, this website has been very useful to me in the past so wanted to thank the community first and foremost. I've been learning Python over the last 2-3 weeks, just by doing my own little "projects", and I had a question that I tried searching for but was not really sure on how to phrase it so finding an answer is a bit difficult.</p>
  
  <p>Essentially what I would like to do is take a user input for a Pokémon, go to a website which has information on it and prints a table of the moves that Pokémon can learn by leveling up. I've managed to get a code running, however, the issue I have is that each Pokémon learns a different number of moves. The code I have is:</p>
  
  <pre><code>import selenium.webdriver as webdriver
  
  def moves(x):
          move = browser.find_element_by_xpath("""//*[@id="svtabs_moves_15"]/div[1]/div[1]/div[1]/table/tbody/tr[""" + str(x) + """]/td[2]/a""").text
          return(move)              
  
  poke = input("Search for which Pokémon?: ")
  browser = webdriver.PhantomJS()
  browser.get("https://pokemondb.net/pokedex/" + str(poke))
  for x in range(1,50):
      print(moves(x))
  </code></pre>
  
  <p>If a Pokémon only has 15 moves it learns by level up, then on the 16th iteration of x, an error is returned because that xpath doesn't exist, so I am looking for a way to modify my code such that it stops printing if the xpath doesn't exist. </p>
  
  <p>I was thinking using a <code>while True</code> statement, but not too sure how to approach it. Again, I'm very new to Python so the code may not be the most elegant. </p>
  
  <p>Thanks for reading!</p>