title: Python throws exception when multiple span text results are present - want to select first instance
body: <p>In the Python code using Selenium and Firefox, I have a list of report names stashed in a file called New.txt that is on the desktop of the computer I am running the code on. The script cycles through these names, finds the name on the website, clicks (twice) and downloads the report. The only issue is when the names of the reports are similar. For example Jacks Report, Report, Report1, Report2, Mary Report. When it gets to Report in the list, it errors out (throws an exception). This makes sense because the code basically is finding three of the same report names.
  I want to specifically alter the problem code below to find the exact report name. When it searches for "REPORT" I want it to only identify that report and not REPORT1 and REPORT2.</p>
  
  <p>Problem:</p>
  
  <pre><code>browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
  </code></pre>
  
  <p>Entire Python code:</p>
  
  <pre><code>for x in range(0, i):      
     while True:    
     try:
  
      x= x
      with open('C:/Users/me/Desktop/New.txt') as t:
          mylist = t.read().splitlines()
      z=mylist[x]
      print z
  
      fp = webdriver.FirefoxProfile('C:/Users/ME/Documents/FirefoxProfile/')
      browser = webdriver.Firefox(fp)
  
      browser.get('https://Websiteofdeath.com')
      browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").clear()
      browser.find_element_by_id("ctl00_PlaceHolderMain_login_UserName").send_keys("USER")
      browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").clear()
      browser.find_element_by_id("ctl00_PlaceHolderMain_login_password").send_keys("PASSWORD")
      browser.find_element_by_id("ctl00_PlaceHolderMain_login_login").click()
      time.sleep(8)
      browser.get('https://Websiteofdeath.com')
      time.sleep(8)
  
      ProgramManagement= browser.find_element_by_partial_link_text('Foo')
      ProgramManagement.click()
      ProgramManagement= browser.find_element_by_partial_link_text('Foob').send_keys(Keys.ARROW_LEFT)
      time.sleep(8)           
  
      browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
      browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
      #Click is mentioned here twice because I need to...click twice!                 
      #browser.find_element_by_xpath('//span[text()="' + z + '"]').click()
  
      time.sleep(30)
      try:
          browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_updateFilters_TitleAnchor").click()            
      except NoSuchElementException:
          pass  
      time.sleep(30)
      browser.find_element_by_css_selector("#ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_ImageAnchor &gt; img").click()
      time.sleep(30)
  
      path = 'C:/Users/ME/Desktop/AUTOREPORTS'
      count = len([g for g in os.listdir(path) if os.path.isfile(os.path.join(path, g))])
  
      if count == x + 1:     # equal
          print "Made the speed"
          pass
      elif count != x + 2:   # not equal
          time.sleep(160)
          print "Waiting a little bit"
          pass
      browser.quit()
      os.rename('/Users/ME/Desktop/AUTOREPORTS/report.csv', '/Users/ME/Desktop/AUTOREPORTS/'+z+'.csv')
      print(count,"of",i)
  except:
      browser.quit()           
      continue
  else:
      break
  </code></pre>
  
  <p>This is the HTML code (snippet) that I am trying to cycle through - notice that the files names are very similar:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;/div&gt;&lt;/li&gt;&lt;li class="rtLI"&gt;&lt;div class="rtMid"&gt;
  &lt;span class="rtSp"&gt;&lt;/span&gt;&lt;img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information" class="rtImg" /&gt;&lt;span title="Key Plan Information" class="rtIn"&gt;Key Plan Information&lt;/span&gt;
  &lt;/div&gt;&lt;/li&gt;&lt;li class="rtLI"&gt;&lt;div class="rtMid"&gt;
  &lt;span class="rtSp"&gt;&lt;/span&gt;&lt;img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information with email addresses" class="rtImg" /&gt;&lt;span title="Key Plan Information with email addresses" class="rtIn"&gt;Key Plan Information with email addresses&lt;/span&gt;
  &lt;/div&gt;&lt;/li&gt;&lt;li class="rtLI"&gt;&lt;div class="rtMid"&gt;
  &lt;span class="rtSp"&gt;&lt;/span&gt;&lt;img src="/_layouts/ecm360/images/ReadReports.gif" alt="Key Plan Information with email addresses and process description" class="rtImg" /&gt;&lt;span title="Key Plan Information with email addresses and process description" class="rtIn"&gt;Key Plan Information with email addresses and process description&lt;/span&gt;
  &lt;/div&gt;&lt;/li&gt;&lt;li class="rtLI"&gt;&lt;div class="rtMid"&gt;</code></pre>
  </div>
  </div>
  </p>