关于selenium加载用户文件保持登录状态
如下脚本是一个微博用selenium'自动化登录的简单例子
import time from selenium import webdriver from loguru import logger from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 # todo 线上线下的需要改值 local = True logger.add('running.log', rotation='00:00') if local: path = ".//chromedriver" else: path = "/root/selenium_env/chromedriver" chrome_options = Options() # 本地在调的时候,可以用由头浏览器 if not local: chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--window-size=1920,1080') chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) # 用来可以读取浏览器配置 chrome_options.add_argument(r'--user-data-dir=C:\Users\tjp40922\AppData\Local\Google\Chrome\User Data') bro = webdriver.Chrome(options=chrome_options) # 在访问url之前就要执行,以隐藏浏览器指纹 bro.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) # 创建隐式等待对象 wait = WebDriverWait(bro, 15) # 访问微博搜索页面,拿到预登陆的cookie # bro.get('https://weibo.com/login.php') bro.get('https://weibo.com') pre_cookies = bro.get_cookies() time.sleep(50) def login(): try: # # 输入用户名和密码 # 用户名 wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='input_wrap']/input[@id='loginname']"))) user = bro.find_element_by_xpath("//div[@class='input_wrap']/input[@id='loginname']") user.clear() user.send_keys("xxxxxxxxxxxxxxxxxxxxx") logger.info("用户名输入完成") time.sleep(5) bro.save_screenshot("username.png") # 密码 wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='input_wrap']/input[@type='password']"))) pass_Word = bro.find_element_by_xpath("//div[@class='input_wrap']/input[@type='password']") pass_Word.clear() pass_Word.send_keys("xxxxxxxxxxxxxxxxxxx") logger.info("密码输入完成") time.sleep(6) bro.save_screenshot("password.png") # 点击登录按钮 wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='info_list login_btn']/a/span"))) log_btn = bro.find_element_by_xpath("//div[@class='info_list login_btn']/a/span") log_btn.click() logger.info("登录按钮点击完成") time.sleep(8) bro.save_screenshot("yanzhengma1.png") code = input("请输入验证码:").strip() # 输入验证码 wait.until(EC.presence_of_element_located((By.XPATH, "//input[@node-type='verifycode']"))) verifycode = bro.find_element_by_xpath("//input[@node-type='verifycode']") verifycode.clear() verifycode.send_keys(code) time.sleep(6) logger.info("输入验证码完成") time.sleep(5) bro.save_screenshot("yanzhengma2.png") # 再次点击登录 wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='info_list login_btn']/a/span"))) log_btn = bro.find_element_by_xpath("//div[@class='info_list login_btn']/a/span") log_btn.click() logger.info("再次登录按钮点击完成") time.sleep(20) bro.save_screenshot("islogin.png") time.sleep(50) bro.save_screenshot("last_loin.png") time.sleep(120) bro.quit() except: bro.quit() if __name__ == '__main__': login()
其中重要的就是option添加--user-data-dir,加载正常浏览器的缓存目录,包含用户的登录信息等()
chrome_options.add_argument(r'--user-data-dir=C:\Users\xxxxxxxxxx\Chrome\User Data')
注意:当加载用户目录,正常用户打开的浏览器都要关掉,否则会报错
注意:当加载用户目录,正常用户打开的浏览器都要关掉,否则会报错
注意:当加载用户目录,正常用户打开的浏览器都要关掉,否则会报错
这个用户目录在哪里看?
chrome://version/