I'm trying to schedule in task scheduler:
Everything works fine, except the python.exe window will remain open while the command prompt closes.
My batch file: (the sleep is for the python code to run. It takes a few seconds)
call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit
My python script:
driver = webdriver.Chrome(executable_path="C:\path\to\chromedriver")
driver.get('http://website.com')
# Find email and pw fields and then fill them in
email = driver.find_element_by_id("user_email")
email.send_keys('[email protected]')
pw = driver.find_element_by_id("user_password")
pw.send_keys('password')
# Click on sign-in button
driver.find_element_by_class_name("button").click()
time.sleep(5)
# Click on save button to update
driver.find_element_by_class_name("button").click()
# Close driver
driver.close()
Last thing, the program/script is the batch file, no arguments, and the start in is in the directory that the batch file is in.
Any help would be appreciated!
Using Windows Task Scheduler. Step 1: Create a Batch File. Open Notepad and follow this generic structure : “Path where your Python exe is stored\python.exe” “Path where your Python script is stored\script name.py” Finally, save the Notepad with your file name and the “.bat” extension anywhere on your machine, Eg – ‘automation.bat’.
At any given point in time, in addition to prescribed times, the user can simply double-click on the batch file and get the output from Python. Finally, you my want to visit the task scheduler documentations to find out more about scheduling tasks.
For this step, I’m going to use Windows 10 to execute the Python Script via the Windows Scheduler. Similar principles would apply when using previous versions of Windows. First, open the Control Panel and then click on the Administrative Tools: Next, double-click on the Task Scheduler, and then choose the option to ‘Create Basic Task…’
Use Task Scheduler run a batch file. The batch file runs pytest and captures our pytest function results to sys.
put you python codes in a main()
function.
and give:
if __name__ == '__main__':
main()
at the end. Just tested works for me.
@pk2019 's answer really helped me.
One improvement is to use
drv = webdriver.Chrome()
# Do your things.
...
drv.close()
drv.quit()
No need to do the dirty work of killing task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With