Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单独文件中的Python Selenium函数-NameError_Python_Selenium_Selenium Webdriver - Fatal编程技术网

单独文件中的Python Selenium函数-NameError

单独文件中的Python Selenium函数-NameError,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在构建一个Python脚本,并希望将某些函数拆分为单独的文件,以便于维护 我有两个文件,当前名为main.py和function1.py main.pydef #Setup Imports import os import os.path import sys # Import Functions from function1 import myfunction #Setup Selenium from selenium import webdriver from selenium.

我正在构建一个Python脚本,并希望将某些函数拆分为单独的文件,以便于维护

我有两个文件,当前名为main.py和function1.py

main.pydef

#Setup Imports
import os
import os.path
import sys


# Import Functions
from function1 import myfunction


#Setup Selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium import webdriver


#Launch Firefox
def init_driver():
    driver = webdriver.Firefox()
    return driver   

  url_list = ['http://www.example.com/page1', 'http://www.example.com/contact', 'http://www.example.com/about', 'http://www.example.com/test'];

driver = init_driver()

# Init Blank List
checked_urls = []

for url in url_list:
    myfunction(driver)

print(checked_urls)
函数1.py

def myfunction(driver):

    driver.get(url)
    htmlText = driver.find_element_by_css_selector("#phrase").text

    if "This Is My Phrase" in htmlText:
        checked_urls.extend(['PHRASE_FOUND'])
    else:
        checked_urls.extend(['PHRASE_FOUND'])
我试图让它访问列表中的每个URL,并检查页面上的这是我的短语。如果它找到了,那么它应该添加到列表中

我在运行脚本时看到以下错误

NameError: name 'url' is not defined

我很确定这与我导入单独函数的方式有关,但无法找出问题所在,有人能帮忙吗?

您还必须将url变量传递给myfunction:

def myfunction(driver, url):

    driver.get(url)
    htmlText = driver.find_element_by_css_selector("#phrase").text

    if "This Is My Phrase" in htmlText:
        checked_urls.extend(['PHRASE_FOUND'])
    else:
        checked_urls.extend(['PHRASE_FOUND'])
然后在主文件中:

for url in url_list:
    myfunction(driver, url)

您还必须将url变量传递给myfunction:

def myfunction(driver, url):

    driver.get(url)
    htmlText = driver.find_element_by_css_selector("#phrase").text

    if "This Is My Phrase" in htmlText:
        checked_urls.extend(['PHRASE_FOUND'])
    else:
        checked_urls.extend(['PHRASE_FOUND'])
然后在主文件中:

for url in url_list:
    myfunction(driver, url)

我认为有些代码应该更正:

第一次,删除代码前的空白空间> URLListList:

  #url_list = ['http://www.example.com/page1', 'http://www.example.com/contact', 'http://www.example.com/about', 'http://www.example.com/test'];
url_list = ['http://www.example.com/page1', 'http://www.example.com/contact', 'http://www.example.com/about', 'http://www.example.com/test'];
然后,
url
是一个局部变量,不能在函数
myfunction
中直接访问它。但它可以作为函数参数访问:

def myfunction(driver, url):
    ...

我认为有些代码应该更正:

第一次,删除代码前的空白空间> URLListList:

  #url_list = ['http://www.example.com/page1', 'http://www.example.com/contact', 'http://www.example.com/about', 'http://www.example.com/test'];
url_list = ['http://www.example.com/page1', 'http://www.example.com/contact', 'http://www.example.com/about', 'http://www.example.com/test'];
然后,
url
是一个局部变量,不能在函数
myfunction
中直接访问它。但它可以作为函数参数访问:

def myfunction(driver, url):
    ...

这与进口无关!尝试将您的
myfunction()
视为独立函数。。。没有名为
url
的变量。我想您应该将
url
添加到函数args:
myfunction(driver,url)
这与导入无关!尝试将您的
myfunction()
视为独立函数。。。没有名为
url
的变量。我想您应该将
url
添加到函数args:
myfunction(driver,url)
这是有意义的,我是否也需要传递选中的url?您最好在
myfunction()
中声明
checked\uURL=[]
并让它
返回选中的url
这是有意义的,我还需要传递选中的URL吗?你最好在
myfunction()中声明
checked\u URL=[]
并让它
返回选中的URL