I am new to python program, don't know much about the syntax of python. I have written a program which will extract the csv data as below.
<html>
<body>
import csv, sys
filename = "inputfile.csv"
reader = csv.reader(open(filename, "rb"))

#rownum = 0

try:
for row in reader:
#if rownum == 0:
# header = row
#else:
colnum = 0
for col in row:
#print '%-8s: %s' % (header[colnum], col)
if colnum == 2:
if col == 'ISLNK':
print 'Found the Link - %s' % row[colnum+1]
elif col == 'ISTXT':
print 'Found the Text - %s' % row[colnum+1]
elif col == 'CRTLK':
print 'Found the control Link - %s, %s' % (row[colnum+1],row[colnum+2])
elif col == 'IMGCM':
print 'Found the Image comaprison - %s' % row[colnum+1]
elif col == 'IMGLK':
print 'Found the Image Link - %s' % row[colnum+1]
else:
print 'Wrong Data is Provided !'
colnum += 1
#rownum += 1

except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
</body> </html>

Below is another class which uses selenium unittest frame work, i want to pass the values that we got from the csv to the selenium commands. I don't want to hard code the text, link and image source in selenium commands. I don't know how to pass the values from on function to another function. Below is selenium program.
<html>
<body>
from selenium import selenium
import unittest, time, re
class NewTest(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "")
self.selenium.start()

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link=Moviefone")
sel.wait_for_page_to_load("30000")

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link=CityGuide")
sel.wait_for_page_to_load("30000")

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link=News")
sel.wait_for_page_to_load("30000")

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link=Finance")
sel.wait_for_page_to_load("30000")

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link= Asylum")
sel.wait_for_page_to_load("30000")

def test_new(self):
sel = self.selenium
sel.open("/portal/")
sel.click("link=Music")
sel.wait_for_page_to_load("30000")

def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()
</body></html>

Dani AI

Generated

Brief summary and quick fixes: read the CSV once into a simple data structure, then hand that structure to your test code instead of hard‑coding values. is right about preserving indentation and posting code in tags. — another important gotcha in your post is that you defined test_new many times; unittest will only see the last definition. Make one test that loops the CSV rows, or generate separate test functions per row.

A compact pattern that works reliably:

  • parse CSV into a list of records (list of dicts or tuples);
  • store that list on the test class or the test instance (self.actions) so every test method can use it;
  • dispatch each record to a small helper that issues the Selenium command.

Example (illustrative):

def load_actions(path):
    import csv
    actions = []
    with open(path, 'r') as fh:
        for row in csv.reader(fh):
            if not row: 
                continue
            tag = row[2] if len(row) > 2 else None
            params = row[3:]
            actions.append({'tag': tag, 'params': params})
    return actions

import unittest
from selenium import selenium

class NewTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.actions = load_actions('inputfile.csv')

    def setUp(self):
        self.sel = selenium("localhost", 4444, "*chrome", "http://example.com/")
        self.sel.start()

    def test_run_csv_actions(self):
        for a in self.actions:
            if a['tag'] == 'ISLNK':
                self.sel.click("link=%s" % a['params'][0])
                self.sel.wait_for_page_to_load("30000")
            elif a['tag'] == 'ISTXT':
                self.assertIn(a['params'][0], self.sel.get_text("css=body"))
            # add other handlers here

    def tearDown(self):
        self.sel.stop()

Troubleshooting notes: set the parsed data once (use setUpClass for speed), use instance attributes (self.) to share between methods, and keep one test_ method that iterates or generate tests programmatically. Add clear checks for missing columns and use try/except to log CSV parsing errors. Consider migrating from the old Selenium RC API to WebDriver for a more current and robust API.

Please, edit your above post and put whatever is Python code inside tags like this:

[code=python] CODE GOES HERE [/code]

Indenting is essential to Python, so your code needs to retain it in the post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.