Python自动化测试系列[v1.0.0][杀浏览器进程]

杀浏览器进程

Webdriver虽然有quit()方法和close()可以关闭浏览器,但有些时候浏览器进程并不能彻底关闭,我们需要掌握杀进程的方法,代码实例如下:

# encoding = utf-8
from selenium import webdriver
import unittest
import os
from time import sleep
class Test_Kill_Browser(unittest.TestCase):

    def test_kill_browser_process(self):
        # 启动浏览器
        chrome_driver = webdriver.Chrome()
sleep(5)
        firefox_driver = webdriver.Firefox()
sleep(5)
        ie_driver = webdriver.Ie()
        sleep(5)
# 杀chrome浏览器进程
        code = os.system("taskkill /F /iM chrome.exe")
        if code ==0:
            print(u"Kill Firefox Successfully")
        else:
            print(u"Kill Firefox Failed")
        # 杀firefox浏览器进程
        code = os.system("taskkill /F /iM firefox.exe")
        if code ==0:
            print(u"Kill Firefox Successfully")
        else:
            print(u"Kill Firefox Failed")
        # 杀ie浏览器进程
        code = os.system("taskkill /F /iM ie.exe")
        if code ==0:
            print(u"Kill Firefox Successfully")
        else:
            print(u"Kill Firefox Failed")

if __name__ == '__main__':
    unittest.main(verbosity=2)

执行结果

在这里插入图片描述
在执行结果中出现乱码,是因为我们的代码文件的File Encodings设置是默认UTF-8的,想解决这乱码问题,我们可以修改Pycharm的设置,找到Settings,或者直接使用快捷键Ctrl+Alt+S打开Settings
在这里插入图片描述
然后在检索栏中检索encoding,便可以找到File Encodings的选项
在这里插入图片描述
将Global Encoding修改为GBK后,在此执行代码,执行结果如图
在这里插入图片描述

发布了231 篇原创文章 · 获赞 188 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/dawei_yang000000/article/details/105648220
今日推荐