我試圖從調用一個函數FOR循環,但得到的錯誤:裏面調用的函數「for」循環拋出一個NameError
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
:
test()
NameError: name 'test' is not defined
的代碼如下
所以我想調用這個函數,當函數被調用時,我希望數據被複制/粘貼到Excel中。
有人可以幫助我提高代碼嗎?
爲什麼在定義它之前調用函數? –
我在底部定義它。 –
嘗試在頂部定義它。這就是我的意思*在定義它之前調用一個函數*。 –