用selenium 模拟登录知乎

"""
level 2:
4、知乎网站的登录
"""

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\Google\Chrome\Application\chromedriver')
driver.implicitly_wait(5)
driver.maximize_window()
driver.get('https://www.zhihu.com/')
click_btn = driver.find_element_by_xpath('//button[@data-za-detail-view-id="2278"]')
ActionChains(driver).click(click_btn).perform()

username = input("请输入账号")
password = input("请输入密码")
driver.find_element_by_name('username').send_keys(username)
driver.find_element_by_name('password').send_keys(password)
# id="su"是百度搜索按钮,click() 是模拟点击
click_btn1 = driver.find_element_by_xpath('//button[@type="submit"]')
ActionChains(driver).click(click_btn1).perform()

 

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