使用Selenium和Chromedriver自动化登录以提取会话Token November 2024
结合使用selenium库和chromedriver,可以编写一个脚本来模拟用户实现登录过程:使用selenium启动Chrome浏览器,定位到登录页面中的用户名和密码输入框,自动填充账号和密码信息,接着触发登录按钮完成登录操作,登录成功后可提取该Token信息,以便后续的自动化操作或进行身份验证
一、依赖安装 1.1 软件版本
软件
查看版本命令
信息
Python
python3 -V
3.11.9 / 3.9.9
Selenium
pip3 show selenium
4.26.1
chrome
google-chrome –version
130.0.6723.91
chromedriver
chromedriver –version
130.0.6723.91
chrome与chromedriver版本最好保持一致
1.2 安装selenium
1.3 安装chrome
下载安装包
1 sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
安装依赖
1 sudo yum install -y --enablerepo=base google-chrome-stable
如果自动下载GPG密钥失败,你可以尝试手动下载并安装它。使用以下命令:wget https://dl.google.com/linux/linux_signing_key.pub
,sudo rpm --import linux_signing_key.pub
安装chrome
1 sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
1.4 安装chromedriver
去资源网站 根据chrome版本号,下载安装包1 wget https://storage.googleapis.com/chrome-for-testing-public/130.0.6723.91/linux64/chromedriver-linux64.zip
解压移动资源1 2 unzip chromedriver-linux64.zip cp chromedriver /usr/local/bin/
二、执行脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.chrome.options import Optionsimport timeUSERNAME = 'admin' PASSWORD = 'admin' URL = 'http://127.0.0.1/#/login' chromedriver_path = '/usr/local/bin/chromedriver' service = Service(executable_path=chromedriver_path) chrome_path = '/opt/google/chrome/chrome' options = Options() options.add_argument('--no-sandbox' ) options.add_argument('window-size=1920x3000' ) options.add_argument('--disable-gpu' ) options.add_argument('--hide-scrollbars' ) options.add_argument('blink-settings=imagesEnabled=false' ) options.add_argument('--headless' ) driver = webdriver.Chrome(service=service, options=options) def get_info_from_local_storage (): localStorageMap = {} localStorageItems = driver.execute_script("return Object.keys(localStorage);" ) print (localStorageItems) for key in localStorageItems: value = driver.execute_script(f"return localStorage.getItem('{key} ');" ) localStorageMap[key] = value for key, value in localStorageMap.items(): print (f'{key} : {value} ' ) try : driver.get(URL) username_input = driver.find_element(By.CSS_SELECTOR, 'input[type="text"]' ) username_input.send_keys(USERNAME) password_input = driver.find_element(By.CSS_SELECTOR, '.ivu-input.ivu-input-large[type="password"]' ) password_input.send_keys(PASSWORD) login_button = driver.find_element(By.XPATH, "//button[@type='submit']" ) login_button.click() login_button = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]' ) login_button.click() time.sleep(2 ) get_info_from_local_storage() finally : driver.quit()
注意点:
options某些参数若不设置可能会出现异常错误,例如SessionNotCreatedException等;
元素定位选择可尝试多种方式,若使用placeholder在Linux环境中not found.
发布时间: 2024-11-01 15:57:10
更新时间: 2024-11-01 15:59:51
本文链接: https://wyatt.ink/posts/Tools/1130652765.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!