Selenium的使用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。
    安装:
        pip3 install selenium
    优缺点:
        优:无需再自己操作cookie和header
        缺:慢
    依赖驱动:
        Firefox
            https://github.com/mozilla/geckodriver/releases
        Chrome
            http://chromedriver.storage.googleapis.com/index.html
"""

from selenium import webdriver

# 配置驱动
option = webdriver.ChromeOptions()
driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver', chrome_options=option)

# 1. 控制浏览器打开指定页面
driver.get("https://dig.chouti.com/all/hot/recent/1")


# 2. 找到登录按钮
btn_login = driver.find_element_by_xpath('//*[@>)
# 3. 点击按钮
btn_login.click()

# 4. 找到手机标签
input_user = driver.find_element_by_xpath('//*[@>)
# 5. 找到密码标签
input_pwd = driver.find_element_by_xpath('//*[@>)

# 6. 输入用户名
input_user.send_keys('13121758648')
# 7. 输入密码
input_pwd.send_keys('woshiniba')


# 8. 点击登录按钮
input_submit = driver.find_element_by_xpath(
    '//*[@>)
input_submit.click()

print(driver.get_cookies())

# 9. 点击跳转
news = driver.find_element_by_xpath('//*[@>)
# news.click()
driver.execute_script("arguments[0].click();", news)

# 10.管理浏览器
driver.close()
View Code

相关文章: