selenium模拟登陆

以模拟登陆csdn账号为例:
import time

from selenium import webdriver

(创建谷歌浏览器对象)
driver = webdriver.Chrome(r"下载的webdriver本地地址")
driver.get(‘https://passport.csdn.net/account/login’)

driver.find_element_by_xpath(’//*[@id=“app”]/div/div/div/div[2]/div[2]/ul/li[1]/a’).click()
time.sleep(3)

用户名

username = driver.find_element_by_id(‘all’)

密码

password = driver.find_element_by_id(‘password-number’)

username.send_keys(“用户名”)
time.sleep(1)
print("---------------------------")
password.send_keys(“密码”)

登陆

driver.find_element_by_xpath(’//*[@id=“app”]/div/div/div/div[2]/div[2]/form/div/div[6]/div/button’).click()
time.sleep(2)

获取登陆后的cookie

cookies = driver.get_cookies()
print(“cookies:”,cookies)

你可能感兴趣的:(爬虫)