Calling a function inside 'For' loop throw

2019-08-08 08:26发布

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 ?

1条回答
家丑人穷心不美
2楼-- · 2019-08-08 09:03

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).

查看更多
登录 后发表回答