import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#通过调用cmd打开chrome, 端口号9222
def open_chrome():
global open_chrome_status
if open_chrome_status == 0:
os.popen('chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"') #命令行打开Chrome
time.sleep(1)
open_chrome_status = 1 #修改Chrome打开状态 避免重复打开
#锁定现在打开的Chrome
def lock_chrome():
global driver
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" #路径为chromedriver的保存位置
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
return driver
open_chrome()
lock_chrome()
## 在此后继续自己的程序,就可以在打开的Chrome 继续代码了