Python automated testing series [v1.0.0] [kill browser process]

Kill browser process

Although Webdriver has the quit () method and close () to close the browser, sometimes the browser process cannot be completely closed. We need to master the method of killing the process. The code examples are as follows:

# 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)

Results of the

Insert picture description here
The garbled characters appear in the execution result because the File Encodings setting of our code file is the default UTF-8. To solve this garbled problem, we can modify the settings of Pycharm, find Settings, or directly use the shortcut key Ctrl + Alt + S Open Settings
Insert picture description here
and search encoding in the search bar, you can find the option of File Encodings.
Insert picture description here
After modifying Global Encoding to GBK, execute the code here, and the execution result is shown in the figure
Insert picture description here

Published 231 original articles · praised 188 · 120,000 views

Guess you like

Origin blog.csdn.net/dawei_yang000000/article/details/105648220