一、各模块说明
selenium.webdriver 浏览器驱动 selenium.webdriver.common.by 选择器类型 selenium.webdriver.support.wait.WebDriverWait 等待控制 selenium.webdriver.support.expected_conditions 条件控制 selenium.common.exceptions 异常类型
二、导入相关模块
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
三、加载驱动
# 按驱动类型加载驱动 if Path('./chromedriver.exe').exists(): driver = webdriver.Chrome() elif Path('./geckodriver.exe').exists(): driver = webdriver.Firefox() else: print('异常:未找到浏览器驱动') print('提示:请下载对应版本的浏览器驱动,并放置于目录:' + os.getcwd()) print('chrome: http://npm.taobao.org/mirrors/chromedriver/') print('Firefox: http://npm.taobao.org/mirrors/geckodriver/') exit(0)
四、打开网页
# 窗口最大化 driver.maximize_window() # 打开网页 driver.get('https://xxxxx')
五、元素获取及点击
driver.find_element(By.CSS_SELECTOR, '.btn-login').click()
注:这里用的是 css 选择器,其他选择器参考这里:定位元素 | Selenium
六、输入账号密码并提交登录
driver.find_element(By.CSS_SELECTOR, '.phonenum_input').send_keys(phone) driver.find_element(By.CSS_SELECTOR, '.password_input').send_keys(password) driver.find_element(By.CSS_SELECTOR, '.login_submit').click()
七、等待登录成功后获取cookie
locator = (By.CSS_SELECTOR, '.user') encrypted = '%s****%s' % (phone[:2], phone[-2:]) WebDriverWait(driver, 3).until(EC.text_to_be_present_in_element(locator, encrypted)) cookies = driver.get_cookies() Path('./cookies.txt').write_text(json.dumps(cookies)) print('登录成功') driver.quit()
八、使用保存的cookie请求其他接口
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62'} cookies = Path('./cookies.txt').read_text() cookies = {x['name']: x['value'] for x in json.loads(cookies)} try: r = requests.get(url, data=data, headers=headers, cookies=cookies, timeout=5) return r.json() except requests.exceptions.Timeout: print('Error: request timeout.') return None except json.decoder.JSONDecodeError: print('Error: json decode error.') return None
九、selenium 的其他操作
https://www.selenium.dev/zh-cn/documentation/webdriver/browser_manipulation/
完。