title: Python Selenium Loop
body: <p>I am looking to loop this script using a list of values. I have an excel doc with all the <code>.send_keys</code> values I would like to use. I was going to leverage openpyxl to pull a list in python. I also can define the list in python and not use openpyxl as well if needed.</p>
  
  <p>Currently I have to copy paste the whole script twice just because I want this script to run with these different send key variables.</p>
  
  <pre><code>driver.find_element_by_name("t1st__TBOX").send_keys("619272")
  driver.find_element_by_name("t1st__TBOX").send_keys("894323")
  </code></pre>
  
  <p>I have ~100 unique variables so I am looking for a way for <code>.send_keys</code> to just reference a list and inject the value and re run the script.</p>
  
  <p>How can I loop this script so it just references a list that has multiple send_keys variables?</p>
  
  <p>Any insight on something that would work would be great!</p>
  
  <pre><code># -*- coding: utf-8 -*-
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.support.ui import Select
  from selenium.common.exceptions import NoSuchElementException
  from selenium.common.exceptions import NoAlertPresentException
  from selenium.webdriver.support import expected_conditions as EC
  import unittest, time, re
  
  
  class Test1(unittest.TestCase):
  def setUp(self):
      self.driver = webdriver.Chrome()
      self.driver.implicitly_wait(30)
      self.base_url = "https://www.website.com/"
      self.verificationErrors = []
      self.accept_next_alert = True
  
  def test_1(self):
      driver = self.driver
      driver.get("https://website.com/")
      driver.implicitly_wait(3)
      driver.find_element_by_xpath("//div[5]/div/div[2]/img").click()
      driver.implicitly_wait(3)
      driver.switch_to.window(driver.window_handles[1])
      seq = driver.find_elements_by_tag_name('iframe')
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//tr[3]/td").click()
          except:
              print("It's not: ", x)
              continue
      driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::td[5]").click()
      driver.find_element_by_name("t1st__TBOX").clear()
      driver.find_element_by_name("t1st__TBOX").send_keys("619272")
      print("Number of frames present in the web page are: ", len(seq))
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//span[@id='p1st__PDLink']/img").click()
          except:
              print("It's not: ", x)
              continue
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//tr[@id='p1st__PD_MIRow_1_']/td[3]").click()
          except:
              print("It's not: ", x)
              continue
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//font/font/input").click()
          except:
              print("It's not: ", x)
              continue
      driver.find_element_by_xpath("//font/font/input").click()
      driver.find_element_by_link_text("Q619272").click()
      driver.find_element_by_link_text("Email").click()
      driver.find_element_by_xpath("//*[@id='null']/option[2973]").click()
      # ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=null | label=Doe, John(johndoe@abcde.com)]]
      driver.find_element_by_xpath(
          "(.//*[normalize-space(text()) and normalize-space(.)='Email this Quote'])[1]/following::input[2]").click()
      Select(driver.find_element_by_name("FORMAT")).select_by_visible_text("Text")
      driver.find_element_by_link_text("Send").click()
      driver.close()
      ##Round 2##
      driver = webdriver.Chrome()
      driver.get("https://website.com")
      driver.implicitly_wait(3)
      driver.find_element_by_xpath("//div[5]/div/div[2]/img").click()
      driver.implicitly_wait(3)
      driver.switch_to.window(driver.window_handles[1])
      seq = driver.find_elements_by_tag_name('iframe')
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//tr[3]/td").click()
          except:
              print("It's not: ", x)
              continue
      driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::td[5]").click()
      driver.find_element_by_name("t1st__TBOX").clear()
      driver.find_element_by_name("t1st__TBOX").send_keys("894323")
      print("Number of frames present in the web page are: ", len(seq))
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//span[@id='p1st__PDLink']/img").click()
          except:
              print("It's not: ", x)
              continue
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//tr[@id='p1st__PD_MIRow_1_']/td[3]").click()
          except:
              print("It's not: ", x)
              continue
      print("Number of frames present in the web page are: ", len(seq))
      driver.switch_to.default_content()
      for x in range(2):
          try:
              driver.switch_to.frame(x)
              driver.find_element_by_xpath("//font/font/input").click()
          except:
              print("It's not: ", x)
              continue
      driver.find_element_by_xpath("//font/font/input").click()
      driver.find_element_by_link_text("Q894323").click()
      driver.find_element_by_link_text("Email").click()
      driver.find_element_by_xpath("//*[@id='null']/option[2973]").click()
      # ERROR: Caught exception [ERROR: Unsupported command [addSelection | id=null | label=doe, jane(janedoe@abcde.com)]]
      driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Email this Quote'])[1]/following::input[2]").click()
      Select(driver.find_element_by_name("FORMAT")).select_by_visible_text("Text")
      driver.find_element_by_link_text("Send").click()
      driver.close()
  
  def is_element_present(self, how, what):
      try:
          self.driver.find_element(by=how, value=what)
      except NoSuchElementException as e:
          return False
      return True
  
  def is_alert_present(self):
      try:
          self.driver.switch_to_alert()
      except NoAlertPresentException as e:
          return False
      return True
  
  def close_alert_and_get_its_text(self):
      try:
          alert = self.driver.switch_to_alert()
          alert_text = alert.text
          if self.accept_next_alert:
              alert.accept()
          else:
              alert.dismiss()
          return alert_text
      finally:
          self.accept_next_alert = True
  
  def tearDown(self):
      self.driver.quit()
      self.assertEqual([], self.verificationErrors)
  
  
  if __name__ == "__main__":
  unittest.main()
  </code></pre>