selenium免密登录

1,先模拟登录获取cook
2,再用拿到的cook登录

import json
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_argument('--headless')
chrome_options.add_argument(
    'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')

driver = webdriver.Chrome(
    executable_path=r'C:\Program Files\Google\Chrome\Application\chromedriver.exe',chrome_options=chrome_options)



driver.get('https://wcn/')
# sleep等待页面完全加载出来,这一步很关键
time.sleep(6)

driver.maximize_window()


driver.find_element(By.ID, 'user_login').send_keys('4')

# 设置密码
driver.find_element(By.ID, 'user_password').send_keys('020')
time.sleep(20)
# 设置验证码
# wd.find_element(By.NAME, 'code').send_keys('1111')
# wd.find_element(By.ID, 'agree_reg').click()
# 点击提交按钮
driver.find_element(By.NAME, 'commit').click()
# 等5秒
time.sleep(5)

print(driver.current_url)
print(driver.get_cookies())
got_cookie = driver.get_cookies()

format_cookie = ''.join([f'{i["name"]}={i["value"]}; ' for i in got_cookie])[:-2]
print(format_cookie)

登录

from selenium import webdriver
import json
import time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# chrome_options.add_argument('--headless')
chrome_options.add_argument(
    'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')

driver = webdriver.Chrome(
    executable_path=r'C:\Program Files\Google\Chrome\Application\chromedriver.exe',chrome_options=chrome_options)
driver.get('https://wes.cn/')
driver.implicitly_wait(10)
driver.delete_all_cookies()
cookies=[{'domain': 'web.cams.cn', 'httpOnly': True, 'name': '_astraeus_session', 'path': '/', 'secure':'}]
for cookie in cookies:
    if 'expiry' in cookie:
        del cookie['expiry']
    driver.add_cookie(cookie)
driver.refresh()
time.sleep(2)
driver.get('https://www-scien02157')
time.sleep(2)





你可能感兴趣的:(爬虫,selenium,chrome,python)