how to run the selenium with python script for login using command line - python-3.x

here is my code
def test_login(self):
URL = (sys.argv[0])
Username = (sys.argv[1])
Password = (sys.argv[2])
self.driver.get(URL)
print ("Browser launched")
self.driver.find_element(By.NAME, "username").send_keys(Username)
self.driver.find_element(By.CLASS_NAME, "btn-login-submit").click()
time.sleep(7)
self.driver.find_element(By.NAME, "password").send_keys(Password)
self.driver.find_element(By.CLASS_NAME, "btn-login-submit").click()
time.sleep(10)
print('login successful')
how to run in the vscode terminal
i could not get the proper command line to run in terminal

Try the following:
Copy Paste the code in a .py file say test.py
I do not see the calling of the function (test_login). So call it or remove the function if it is not necessary.
in the VS code terminal, type this command Python3 test.py to execute your python script.

Related

How to run a python function from Windows 10 command prompt in 'python filename.py funcname' format?

I have the following python file, named hello.py:
print('Hello')
def world():
print('Hello World')
If I type the following within the command prompt for windows 10 I get:
In: python hello.py
Out: 'Hello'
In: python hello.py world
Out: 'Hello'
Notice in the second command prompt, 'Hello World' is not an output. Any reason on why my file can't execute the python function when called in the command line but is able to load the file no problem?
Why do I need this?: I need to submit a larger file that has many functions for a project. For this file to be automatically graded I need to be able to call any given function from it using the specific sequence 'python filename.py functionname.'
Update: Some of the answers below do work, but they do not follow the specific sequence of 'python filename.py functionname' required for my project to be graded. I appreciate the insights that everyone has provided so far.
You can run functions directly from the command prompt by using:
python -c 'from hello import world; world()'
You can run the python interpreter as an alternative. cd to your file directory and launch python:
python
and then import the function from your file:
from hello import world
(Launching the python cmd from the same directory as your file.)
Then, you can run:
>>> world()
Hello World
Try this piece of code and type hello.py only:
print('Hello')
def world():
return'Hello World'
world()
##################################################################
def F1():
print('This is the first function.')
if __name__ == '__main__':
F1()
def F2():
print('This is the second function.')
if __name__ == '__main__':
F2()
now you can use form(file_name) import(function_name) create folder(1) with multiple functions in it and you can try to import the desired function into another folder(2) one at a time not all of them all at once by using this.
pardon me for the mistakes if found any feel free to ask further if this does not elaborate.
I found a solution that works for me:
After digging deeper, I realized that windows passes a sys.argv input that contains a string of all the command prompt inputs used to call the file.
In my case of 'python hello.py world', the system would pass ['hello.py','world'] as the argument for sys.argv. By creating a dictionary of callable functions, and then matching the string of the sys.argv with its respective function in the dictionary, I am able to execute the code as desired.
New code:
print('Hello')
def world():
print('Hello World')
import sys
callable_functions = {'world':world}
callable_functions[sys.argv[1]]()
Command prompt input and output now:
In: python hello.py world
Out: 'Hello'
Out: 'Hello World'

Opening a Jupyter Notebook from Python Script on Windows cmd line

I have a simple tkinter GUI with a dropdown, that when selected, runs a function. I know the anaconda prompt and cmd line are different, however, I can't get the command to work inside the python script without using the external batch file.
I am reading from a folder of csv files to get the list and all that is fine.
Currently, this works:
import tkinter as tk
import os
folder = 'C:/Users/[user]/Desktop'
OptionList = [fname for fname in os.listdir(folder) if fname.endswith('.csv')]
app = tk.Tk()
app.geometry('500x200')
variable = tk.StringVar(app)
variable.set(OptionList[0])
opt = tk.OptionMenu(app, variable, *OptionList)
opt.config(width=500, font=('Helvetica', 12))
opt.pack(side="top")
def callback(*args):
os.system('cmd /k "C:\\Users\\[user]\\Desktop\\notebook_launch.bat"')
app.destroy()
variable.trace("w", callback)
app.mainloop()
Here is the working batch file:
#echo on
call C:\ProgramData\Anaconda3\Scripts\activate.bat
voila "C:\Users\[user]\Documents\notebook.ipynb"
When I try to execute the commands from windows cmd, if I use /k instead of /c to keep the window open, I can see that the anaconda prompt opens, however the next line of code does not execute.
Attempt at getting both commands to run in the script:
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Alternate attempt, same result:
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat" && "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Noted above, I have also tried using && to combine the commands in one line, but same result, only the anaconda prompt opens and the "voila" command does not execute.
Anyone know what I'm missing or if it possible? My guess is that since the (base) prompt is opening, the os.system function doesn't know what to do with the anaconda prompt. Any help is appreciated.
EDIT:
Turns out I just needed to pause the execution for half a second, as it was executing too quickly. By adding import time and squeezing time.sleep(.5) between the two lines, everything functioned perfectly.
Functioning code:
import time
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
time.sleep(.5)
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()
Turns out I just needed to pause the execution for half a second, as it was executing too quickly. By adding import time and squeezing time.sleep(.5) between the two lines, everything functioned perfectly.
Functioning code:
import time
def callback(*args):
os.system('cmd /c "C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat"')
time.sleep(.5)
os.system('cmd /c "voila C:\\Users\\[user]\\Documents\\notebook.ipynb"')
app.destroy()

Can i access the variables of the python script after run that python script with console?

I run the python script using terminal command
python3 myScript.py
It's simply run my program but if i want to open python console after complete run of my script so that i can access my script's variables.
So, What should i do ? and How can i get my script's variables after run the code using terminal ?
Open a python terminal (type 'python' in cmd);
Paste this (replace 'myScript.py' with your script filename):
def run():
t = ""
with open('myScript.py') as f:
t = f.read()
return t
Type exec(run()). Now you will have access to the variables defined in myScript.py.
I needed to do this so I could explore the result of a request from the requests library, without having to paste the code to make the requests every time.
Make the program run the other program you want with the variables as arguments. For example:
#program1
var1=7
var2="hi"
import os
os.system("python %s %d %s" % (filename, var1, var2))
#program2
import sys
#do something such as:
print(sys.argv[1]) #for var1
print(sys.argv[2]) #for var2
Basically, you are running program2 with arguments that can be referenced later.
Hope this helps :)

Why does resizeColumnToContents work interactively at the python prompt, but not in a PyQt script?

I've been working on a filebrowser application, and I'd like the first column (file name) to be resized properly on startup. I can type the following code at the python prompt and the column resizes properly, but when I put it in a file and try to run it, the column is not resized. Any idea why?
#!/bin/env python
import sys
import os
from PyQt4.QtGui import *
app = QApplication(sys.argv)
treeView = QTreeView()
fileSystemModel = QFileSystemModel(treeView)
rootDir = fileSystemModel.setRootPath(os.path.expanduser('~'))
treeView.setModel(fileSystemModel)
treeView.setRootIndex(rootDir)
treeView.setGeometry(100,100,1024,768)
treeView.show()
treeView.resizeColumnToContents(0)
app.exec_()
Of course, when I copy it to the python prompt, I leave off the app.exec_(). Is that what is causing the column to not resize? (EDIT: I copied "app.exec_()" to the prompt and it did pretty much what you'd expect - the event loop started, and I was able to use the app, then close it, and then I was returned to the python prompt.)
It seems that replacing the call to treeView.resizeColumnToContents(0) with treeView.header().setResizeMode(0, QHeaderView.ResizeToContents) results in the column being expanded when run from the Python prompt and from a script. I have no clue why resizeColumnToContents is not working as intended.
Side note: should #!/bin/env python be #! /usr/bin/env python? At least on my distro, /bin/env doesn't exist.

Komodo edit 8 - Python3 user input

I am having on going issues with this new editor to try and run code with user input. I have selected the python3 interpreter in the preferences: /usr/local/bin/python3
I have also set a shebang at the top of a .py file and I am using the following in my run command -
%(python) "-u" "%F"
However, I am still unable to use input() in my code. In the Komodo Edit Command output from this code:
#!/usr/bin/env python3
print ("Name please?")
name = input()
print ("Hello ", name)
print ("Finish script...")
I get the following:
Name please?
('Hello ', <built-in function input>)
Finish script...
I'm sure it's something simple, but i'm stumped :(
Instead of:-
print ("Name please?")
name = input()
Try this:-
name=input("Name Please?")

Resources