1

I'm trying to call a function from a FOR loop but get the error:

 test()
NameError: name 'test' is not defined

The code is given below:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from lxml import html
import requests
import xlwt

browser = webdriver.Firefox() # Get local session of firefox

# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it

browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")


for link in links:
    link.send_keys(Keys.CONTROL + Keys.RETURN)
    link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
    time.sleep(5)
    test()
    link.send_keys(Keys.CONTROL + 'w')



def test(self):#test function
    elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")

    for elem in elems:
        print elem.text
    elem1 = browser.find_elements_by_css_selector("div.content.clearfix>div>fieldset>div>ul>li>a")

    for elems21 in elem1:
        print elems21.text 
    return 0

So I want to call the function and when the function is called I want the data to be copy/pasted into Excel.

Can somebody help me to enhance the code ?

Python Learner
  • 129
  • 1
  • 11

1 Answers1

1

You have to create a function object before you can use it. In your case you call a function but it is not yet existing hence not defined. As Kevin said, define the function and then try to call it.

UPD: I cannot add comments so I update it here. Mark Lutz in his "Learning Python" book describes it in a great detail, how functions work, what def does and what happens when you call a function. But I assume any other Python book will do the same.

UPD: It is not easy to write comments, so I update the answer.

As said the problem is that you define function after you call it. For example, let's assume I want to write a program which writes "Have fun, " + any name. For simplicity name is given in the program. Option 1: If I write the program like you did meaning 1) call a function 2) define a function I will get an NameError exactly like you get.

Program:

greet = 'Have fun, '

print(greet + name('John')) # I call a function 'name'

def name(x):                # I define a function
    return str(x)

The output will be:

Traceback (most recent call last):
  File "C:/Users/nikolay.dudaev/Documents/Private/deffun2.py", line 3, in <module>
    print(greet + name('John'))
NameError: name 'name' is not defined

ALL that I need to do is change places of function definition and calling a function:

greet = 'Have fun, '

def name(x):                # I define a function
    return str(x)

print(greet + name('John')) # I call a function 'name'

And now the output is:

======= RESTART: C:/Users/nikolay.dudaev/Documents/Private/deffun2.py =======
Have fun, John
>>> 

Here you go!

Copy what you have after def and paste it before your for loop and it should work (though I did not try your code).

Nikolay Dudaev
  • 372
  • 2
  • 8
  • pretty newbie to python if u can just tell me why the error. how can it be resolved ?? – Python Learner Oct 01 '15 at 11:59
  • added the whole code in a class with self after reffering to this [Link from stack SO](http://stackoverflow.com/questions/14804084/python-nameerror-name-is-not-defined). – Python Learner Oct 01 '15 at 12:21
  • As you see in that other question, it is the same with classes: FIRST you define a class SECOND you use it. – Nikolay Dudaev Oct 01 '15 at 12:24
  • My function does not accept any values in the program. Sorry but its turning out to be very confusing... A better clarity with example will help a lot with functions. If you give me a example it would be great... – Python Learner Oct 01 '15 at 12:41
  • def printme( str ): "This prints a passed string into this function" print str return; ` # Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function")` Its the same format i was doing earlier but now i changed it to using class.... – Python Learner Oct 01 '15 at 12:42
  • Okay... That did the trick.. But my function takes no argument in the pgm. I just want my program to go to the subroutine and run it. def test(self):#test function xxxxxxx XXXXX statements here. But my calling function takes 0 arguments. Also when i run the prog without self i get no data from the web. If you want me to start a new thread i can do that. – Python Learner Oct 01 '15 at 12:57