因为某次工作中写了个脚本要来回跑,还要在本机上做很多其他的事情,所以就上网找了个方法,无头浏览器,这样就不会浏览器来回弹出来,执行用例还会很快。
废话不多说,直接放代码:
#coding=utf-8 """ creted on : 2019-05-06 updated on : 2019-05-06 description : ABtester for splitFlow """ from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By for i in range(1, 5, 1): chrome_options = Options() chrome_options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("https://XXXXXXXXXXXXXXXXXXXXXXXXXXXX") time.sleep(1.5) try: WebDriverWait(driver, 3, 0.5).until( EC.presence_of_element_located((By.ID, 'yyyyyy')) #元素id,不便展示,用字符串代替 ) except Exception as e: # raise e print(e) cookies = driver.get_cookies() cookiesq={} for item in cookies: cookiesq[item['name']] =item['value'] print(str(i)+":"+cookiesq['XXXXX']) #cookie名称,不便展示,用字符串代替 driver.delete_all_cookies() if driver.get_cookies()!= []: driver.delete_all_cookies() print("========="+str(driver.get_cookies())) driver.close()