Reputation: 61
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:\path\to\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!
Upvotes: 5
Views: 3922
Reputation: 31
@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.
Upvotes: 3
Reputation: 485
I had a similar problem with a Python selenium web scraper running geckodriver.exe
Firefox web driver on Windows (executed via Task Scheduler using a .bat file). The problem is that my geckodriver.exe
process is still running after the Python script is done ... and I think that running process is preventing the Windows command prompt from closing.
In order to test this hunch, I inserted the tasklist
command into the .bat file, both before and after the Python script. It prints out a list of running Windows tasks to the console ... I noticed the geckodriver.exe
file was still running the Python script was finished.
The way to kill a Windows process (using taskkill
) is described in these two different Stackoverflow responses:
Here is the Windows documentation for taskkill
:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
In my case, I added this as the 2nd to last line: taskkill /im geckodriver.exe /f
, and the last line was exit
. That worked.
Upvotes: 0
Reputation: 79
put you python codes in a main()
function.
and give:
if __name__ == '__main__':
main()
at the end. Just tested works for me.
Upvotes: 3
Reputation: 785
I'm not a python expert, but I think you just need to call sys.exit() or quit().
Instead of using sleep and waiting too long or possibly not long enough, call start with the wait option:
call activate python2
start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
exit
If you don't need the batch file to do anything else, you can just start the python script and exit.
Upvotes: 0