title: Python, Selenium, CSV, and UTF-8 (French) characters
body: <p>I have a CSV file that contains French words, such as "immédiatement". I'm using Python plus Selenium Webdriver to write those words into a text field. Basically, using the required Selenium packages plus csv:</p>
  
  <ul>
  <li>Start up Selenium and go to the correct area.</li>
  <li>Open the CSV file.</li>
  <li>For each row:
  
  <ul>
  <li>Get the cell that contains the French word.</li>
  <li>Write that word in the textarea.</li>
  </ul></li>
  </ul>
  
  <p>The problem:</p>
  
  <p>"UnicodeDecodeError: 'utf8' codec can't decode byte 0x82 in position 3: invalid start byte"</p>
  
  <p>I've tried:</p>
  
  <ul>
  <li>declaring "coding: utf-8" at the top of the file, and leaving it out</li>
  <li>once I set a variable to the contents of the cell, appending .decode("utf-8")</li>
  <li>once I set a variable to the contents of the cell, appending .encode("utf-8")</li>
  </ul>
  
  <p>No love.</p>
  
  <p>(I can't set it to "ignore" or "replace", because I need to actually type the word out. It doesn't appear to be Selenium itself, because when I put the list directly in the script, typing goes fine. (I could put this in as a dict in the script, but jesus, <em>why</em>.))</p>
  
  <p>What am I missing?</p>
  
  <p>[edit] Sample CSV content:</p>
  
  <pre><code>3351,Payé/Effectué,Link1
  45922,Plannifié,Link1
  3693,Honoraires par Produit,Link2
  </code></pre>
  
  <p>And generalised code:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.common.keys import Keys
  import unittest, time, re, csv
  
  csvdoc = "C:\path\to\sample.csv"
  
  class Translations(unittest.TestCase):
      def setUp(self):
          self.driver = webdriver.Firefox()
          self.driver.implicitly_wait(30)
          self.base_url = "https://baseURL.com/"
          self.verificationErrors = []
          self.accept_next_alert = True
  
      def test_translations(self):
          driver = self.driver
          driver.get(self.base_url + "login")
          driver.find_element_by_id("txtUsername").clear()
          driver.find_element_by_id("txtUsername").send_keys("username")
          driver.find_element_by_id("txtPassword").clear()
          driver.find_element_by_id("txtPassword").send_keys("password")
          driver.find_element_by_id("btnSubmit").click()
          # Navigate to the correct area.
          # - code goes here -
          # Open the file and get started.
          with open(csvdoc, 'r') as csvfile:
              csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
              for row in csvreader:
                  elmID = row[0]
                  phrase = row[1]
                  arealink = row[2]
                  driver.find_element_by_xpath("//a[text()='%s']" % arealink).click()
                  time.sleep(1)
                  driver.find_element_by_id(elmID).clear()
                  driver.find_element_by_id(elmID).send_keys(phrase)
                  driver.find_element_by_id("btnSavePhrase").click()
  
      def is_element_present(self, how, what):
          try: self.driver.find_element(by=how, value=what)
          except NoSuchElementException, e: return False
          return True
  
      def tearDown(self):
          self.driver.quit()
          self.assertEqual([], self.verificationErrors)
  
  if __name__ == "__main__":
      unittest.main()
  </code></pre>