pass selenium webdriver as argoument for python program - python

i want to run a python program first.py by another program and pass the webdriver
element as an argoument using subprocess doing so:
driver = webdriver.Firefox(executable_path="geckodriver.exe")
process = subprocess.Popen(["python.exe", 'first.py', driver], shell=False)
but python refuses to pass a webdriver element as an argoument
and gives this error:
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
I'm working on windows 10 and python 3.7

The arguments to the subprocess.Popen call are supposed to be strings whereas you are passing a Python object. This is why you are getting an iteration error - Python attempts to parse the string arguments (that are separated either with a space or a tab) and fails when he gets a Python object.
You have to unfortunately to rethink your strategy. You could construct the driver within the first.py module or have a separate Python module which you could import to get a driver set up when executing the first.py module.

Related

Python execute a function in parallel in loop

I tried to improve the execution time of a script which import datas from CSV into Graphite/Go-Carbon DB time series.
this is the loop which parse all zipfiles and read them in function (execute_run) :
It tried this code but i got an error:
for idx4, Lst_f in enumerate(full_csvfile_paths):
if lst_metrics in Lst_f:
zip_file = Lst_f
with zipfile.ZipFile(zip_file) as zipobj:
print("Using ZipFile:",zipobj.filename)
#execute_run(zipobj.filename, confcsv_path, storage_type, serial)
output = subprocess.run(execute_run(zipobj.filename, confcsv_path, storage_type, serial),stdout=subprocess.PIPE)
print ("Return code: %i" % output.returncode)
print ("Output data: %s" % output.stdout)
Error:
Traceback (most recent call last):
File "./02-pickle-client.py", line 451, in <module>
main()
File "./02-pickle-client.py", line 361, in main
output = subprocess.run(execute_run(zipobj.filename, confcsv_path, storage_type, serial),stdout=subprocess.PIPE)
File "/usr/lib64/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.6/subprocess.py", line 1240, in _execute_child
args = list(args)
TypeError: 'NoneType' object is not iterable
Is there a way to execute X times the function :"execute_run" and control the correct running.
Many thanks for help.
The problem could be that the parallel processes is not set up to handle iterables correctly. Instead of subprocess.run, I would recommend using
multiprocessing.pool or multiprocessing.starmap as specified in these docs.
This could look something like this:
import multiprocessing as mp
# Step 1: Use multiprocessing.Pool() and specify number of cores to use (here I use 4).
pool = mp.Pool(4)
# Step 2: Use pool.starmap which takes a multiple iterable arguments
results = pool.starmap(My_Function, [(variable1,variable2,variable3) for i in data])
# Step 3: Don't forget to close
pool.close()

TypeError when calling expect method of pexpect module in Python 3

I am trying to use pexpect module (version 3.3) with Python 3.4.0. I get an error
TypeError: must be str, not bytes
when I call child.expect method.
Actual code is standard example from pexpect documentation:
child = pexpect.spawn('ssh foo#bar.com')
index = child.expect([pexpect.TIMEOUT, pexpect.EOF, ssh_newkey, '.*password:'])
Exactly the same code works properly with pexpect module (version 3.1), and Python version 2.7.6.
Pexpect documentation on GitHub states that pexpect version 3.3 requires Python 2.6 or 3.2 or above. Does anybody know if pexpect does not work with Python 3 for some reason despite what is stated in documentation for this module?
This is traceback output that I get:
Traceback (most recent call last):
File "/home/sambo9/python/python3-pexpect.py", line 17, in <module>
main()
File "/home/sambo9/python/python3-pexpect.py", line 13, in main
child.expect('.*password:')
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1451, in expect
timeout, searchwindowsize)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1466, in expect_list
timeout, searchwindowsize)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1535, in expect_loop
c = self.read_nonblocking(self.maxread, timeout)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 985, in read_nonblocking
self._log(s, 'read')
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 908, in _log
second_log.write(s)
File "/usr/lib/python3.4/idlelib/PyShell.py", line 1339, in write
raise TypeError('must be str, not ' + type(s).__name__)
TypeError: must be str, not bytes
Additionally, I also see "OpenSSH" box pop open via GUI on Ubuntu prompting for password when I run the script. This does not happen under Python 2.7.6. In Python 2.7, I can login into system without any manual interaction - everything happens automatically via script.
From the documentation:
# In Python 3, spawnu should be used to give str to stdout:
child = pexpect.spawnu('some_command')
child.logfile = sys.stdout

Error using subprocess.check_output in Python

I am at a very basic level in Python, and I'm trying to learn how to use the subprocess module. I have a simple calculator program called x.py that takes in a number, multiplies it by 2, and returns the result. I am trying to execute that simple program from IDLE with the following two lines of code, but I get errors. The number 5 is the number I'm trying to feed into x.py to get a result. Would someone mind helping me understand what I'm doing wrong and help me get it right? Thanks!
import subprocess
result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])
File "C:\Python27\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 855, in _execute_child
args = list2cmdline(args)
File "C:\Python27\lib\subprocess.py", line 587, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
Pass 5 as a string:
import sys
result = subprocess.check_output([sys.executable, "C:\\Users\\Kyle\\Desktop\\x.py", '5'])

Passing arguments while running selenium webdriver for Python

I am using selenium python webdriver for my python test cases.
I have a python script NowSpots_Traffic.py which I'm trying to run from Ubuntu terminal (something similar to command prompt in windows)
python /home/vijay/Projects/SeleniumScripts/NowSpots_Traffic.py samuel secrete
where samuel and secrete are two arguments which I need to pass to my python script NowSpots_Traffic.py
I followed the instructions found at Running Tests in Python with Selenium 2 and WebDriver
section "Multiple browsers and multiple platforms".
But I get the following error message
Traceback (most recent call last):
File "/home/vijay/Projects/SeleniumScripts/NowSpots_Traffic.py", line 43, in <module>
unittest.main()
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 152, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 161, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'samuel'
The Unittest.main() function uses the arguments you pass in to determine test cases to run. See here: Test Discovery
If your script also needs arguments, you will want to remove them from sys.argv before calling
unittest.main()

Can't rm -r directory using python subprocess.call

Welp, I need to remove some huge temporary directories from python and I can't seem to use rm -r. I'm working thought a big dataset (on s3) I don't have the disc space to leave them around.
The usual way I would call a command from python is
import subprocess
subprocess.call('rm','-r','/home/nathan/emptytest')
That gives me
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
What's this all about?
You're calling it the wrong way. The first argument should be a list:
import subprocess
subprocess.call(['rm','-r','/home/nathan/emptytest'])
You might also just want to try and use shutitl.rmtree
According to the documentation,
>> In [3]: subprocess.call?
Type: function
Base Class: <type 'function'>
String Form: <function call at 0x01AE79F0>
Namespace: Interactive
File: c:\python26\lib\subprocess.py
Definition: subprocess.call(*popenargs, **kwargs)
Docstring:
Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
So try:
subprocess.call(['rm','-r','/home/nathan/emptytest'])

Categories

Resources