import time from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC import reclasshello:def__init__(self,url,selepath='c:\\1\\selenium\\chromedriver\\98\\chromedriver.exe'):self.url=urlself.seleniumpath=selepathser = Service(self.seleniumpath)"""这个是新版本的selenium指定的语法,以前是用Options在这里还能自己加上 header 等语句,后面会找例子解释"""self.driver=webdriver.Chrome(service = ser)"""无头模式 --headless,就是后台运行,不会看见有chrome打开,测试就不打开了包含多种参数from selenium.webdriver.chrome.options import Options chrome_optiOns= Options() #chrome_options.add_argument("--disable-extensions") #chrome_options.add_argument("--disable-gpu") #chrome_options.add_argument("--no-sandbox") # linux only chrome_options.add_argument("--headless") # chrome_options.headless = True # also works driver = webdriver.Chrome(service = ser,optiOns=chrome_options) """self.driver.maximize_window()#self.driver.#窗口最大化self.driver.get(self.url)#打开网页defclosedriver(self):self.driver.close()x=hello('https://www.baidu.com')x.closedriver()