1

I am looking to loop this script using a list of values. I have an excel doc with all the .send_keys 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.

Currently I have to copy paste the whole script twice just because I want this script to run with these different send key variables.

driver.find_element_by_name("t1st__TBOX").send_keys("619272")
driver.find_element_by_name("t1st__TBOX").send_keys("894323")

I have ~100 unique variables so I am looking for a way for .send_keys to just reference a list and inject the value and re run the script.

How can I loop this script so it just references a list that has multiple send_keys variables?

Any insight on something that would work would be great!

# -*- 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([email protected])]]
    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([email protected])]]
    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()
4
  • You already know what to do, create a list in python, or get the values from reading excel, then loop in it. Is this your whole script, just one/two line? Commented Dec 5, 2018 at 4:27
  • Hi Vaibhav conceptually I understand what I need to do, but I am not sure how to code that loop. Also not too sure how to code whats needed using openpyxl to extract a list from excel and use it as a reference point, but I will figure that out once I figure out the loop I suppose. :) For now if I can just figure out a way to loop via a pre defined list that'll work I suppose. Commented Dec 5, 2018 at 8:57
  • the whole process of def test_1 needs to be repeated for every key? Sorry I used selenium a long a ago, I don't quite remember how it works exactly. Commented Dec 5, 2018 at 11:29
  • That is correct. Commented Dec 5, 2018 at 15:06

1 Answer 1

1

You need to read the excel in setUp and create a list/tuple. Then in test_1 loop in that list and and put all the current contents of test_1 inside that loop. That should work.

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
        # Read excel here to create a list,
        self.my_keys = ('809767', `797978`, ..., )  # or just copy paste the contents of excel here and make a list/tuple yourself.


    def test_1(self):
        for key in self.my_keys:
            # all the previous code of test_1 here
            ...
            driver.find_element_by_name("t1st__TBOX").clear()
            driver.find_element_by_name("t1st__TBOX").send_keys(key)  # use varible key here to send different key each loop
            ...
            # all the previous code of test_1 here

This should be enough, might require some changes, because I can't still understand the purpose of methods after test_1, I never see you calling them. Also this test might run for a really long time depending on the number of keys you have to send.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.