将selenium webdriver作为python程序的参数传递
我想由另一个程序运行python程序将selenium webdriver作为python程序的参数传递,python,selenium,Python,Selenium,我想由另一个程序运行python程序first.py,并传递webdriver 元素作为论点,使用子流程这样做: driver = webdriver.Firefox(executable_path="geckodriver.exe") process = subprocess.Popen(["python.exe", 'first.py', driver], shell=False) 但是python拒绝将webdriver元素作为argoument传递 并给出了这个错误:
first.py
,并传递webdriver
元素
作为论点,使用子流程
这样做:
driver = webdriver.Firefox(executable_path="geckodriver.exe")
process = subprocess.Popen(["python.exe", 'first.py', driver], shell=False)
但是python拒绝将webdriver元素作为argoument传递
并给出了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/bo/Desktop/instagrambot/run_bot.py", line 23, in <lambda>
run_button = tk.Button(root , text="Run", command=lambda: Run())
File "C:/Users/bo/Desktop/instagrambot/run_bot.py", line 12, in Run
close_fds=True)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1119, in _execute_child
args = list2cmdline(args)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 530, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'WebDriver' is not iterable
Tkinter回调中出现异常
回溯(最近一次呼叫最后一次):
文件“C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\\uuuuuu init\uuuuuu.py”,第1705行,在调用中__
返回self.func(*args)
文件“C:/Users/bo/Desktop/instagrambot/run_bot.py”,第23行,在
run_button=tk.button(root,text=“run”,command=lambda:run())
文件“C:/Users/bo/Desktop/instagrambot/run_bot.py”,第12行,运行中
关闭(fds=真)
文件“C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”,第775行,在\uuu init中__
恢复信号,启动新会话)
文件“C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”,第1119行,在执行子进程中
args=list2cmdline(args)
文件“C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py”,第530行,在list2cmdline中
needquote=(“”在arg中)或(“\t”在arg中)或非arg
TypeError:类型为“WebDriver”的参数不可编辑
我正在使用windows 10和python 3.7子进程.Popen
调用的参数应该是字符串,而您正在传递python对象。这就是为什么会出现迭代错误——Python试图解析字符串参数(用空格或制表符分隔),但在获取Python对象时失败
不幸的是,你不得不重新考虑你的策略。您可以在
first.py
模块中构造驱动程序
,或者拥有一个单独的Python模块,您可以在执行first.py
模块时导入该模块以设置驱动程序。调用子流程的参数应该是字符串,而您传递的是Python对象这就是为什么会出现迭代错误——Python试图解析字符串参数(用空格或制表符分隔),但在获取Python对象时失败
不幸的是,你不得不重新考虑你的策略。您可以在first.py
模块中构造驱动程序
,或者有一个单独的Python模块,您可以在执行first.py
模块时导入该模块以设置驱动程序。我的问题是,当我想调用process.terminate()函数时,first.py
已关闭,但驱动程序未关闭,我尝试将driver.quit()函数放在finally块中,但它也不起作用。进程终止()绕过finally块关闭程序并关闭驱动程序打开为什么首先需要使用子进程
?我一直在一个简单的Python模块中使用Selenium,最后关闭驱动程序,从来没有遇到过问题如果您担心脚本会引发意外的异常,然后您可以将web驱动程序交互代码包装到try/except/finally
块中,并将驱动程序关闭操作放入finally
中,这样web浏览器将始终关闭,因为我正在处理许多python文件,需要从主程序启动和关闭程序是的,我将驱动程序关闭try/except/finally
中的操作会阻止进程。terminate()
操作会绕过它,但请不要理解为什么我要调用进程。terminate()函数时,“first.py”
已关闭,但驱动程序未关闭,我尝试放置驱动程序。quit()函数在finally块中,但它也不起作用。process.terminate()绕过finally块关闭程序并打开驱动程序为什么首先需要使用子进程
?我一直在一个简单的Python模块中使用Selenium,最后关闭驱动程序,从来没有遇到过问题如果您担心脚本会引发意外的异常,然后您可以将web驱动程序交互代码包装到try/except/finally
块中,并将驱动程序关闭操作放入finally
中,这样web浏览器将始终关闭,因为我正在处理许多python文件,需要从主程序启动和关闭程序是的,我将驱动程序关闭try/except/finally中的操作会阻塞进程。terminate()
操作会绕过它,并且不知道为什么