首页 文章

在任务计划程序中运行批处理文件后,Python.exe保持打开状态

提问于
浏览
3

我正在尝试在任务调度程序中安排:

  • 运行批处理文件

  • 激活conda环境

  • 运行python程序

  • 退出命令提示符

一切正常,但python.exe窗口将在命令提示符关闭时保持打开状态 .

我的批处理文件:(睡眠是为了运行python代码 . 需要几秒钟)

call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit

我的python脚本:

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('myemail@email.com')
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()

最后,程序/脚本是批处理文件,没有参数,并且start in位于批处理文件所在的目录中 .

任何帮助,将不胜感激!

2 回答

  • 1

    把你的python代码放在 main() 函数中 .

    并给:

    if __name__ == '__main__':
        main()
    

    在末尾 . 刚测试过对我有用 .

  • 0

    我不是python专家,但我认为你只需要call sys.exit() or quit() .

    而不是使用睡眠并等待太长时间或可能不够长,请使用wait选项调用start:

    call activate python2
    start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
    exit
    

    如果您不需要批处理文件来执行任何其他操作,则可以启动python脚本并退出 .

相关问题