Python如何基于selenium实现自动登录博客园
2019-12-19 07:07
1546 查看
这篇文章主要介绍了Python如何基于selenium实现自动登录博客园,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
需要做的准备:
本文章是使用Chrome,所以需要Chormedriver.exe,具体的下载过程可以百度查到
Selenium是一种自动化测试工具,能模拟浏览器的行为,所以今天我就模拟一下浏览器登陆博客园的行为。
首先,分析问题,登陆博客园需要做些什么:
1.打开浏览器
2.输入博客园主页的网址
3.点击登陆按钮,等待页面跳转
4.输入账号密码,点击登陆
知道了步骤,接下来我们用代码来实现它:from selenium import webdriverimport time#创建登陆类
from selenium import webdriver import time class Loadup: def __init__(self,username,password): self.username = username self.password = password self.driver = webdriver.Chrome() def closeBrowser(self): self.driver.close() def login(self): driver = self.driver driver.get('https://www.cnblogs.com/') time.sleep(2) # 使用xpath的方法来定位元素 login_button = driver.find_element_by_xpath("//a[@onclick='login();return false']") login_button.click() time.sleep(2) # 使用xpath的方法来定位元素 username_elem = driver.find_element_by_xpath("//input[@name='LoginName']") # 使用xpath的方法来定位元素 username_elem.clear() username_elem.send_keys(self.username) # 使用xpath的方法来定位元素 password_elem = driver.find_element_by_xpath("//input[@name='Password']") password_elem.clear() password_elem.send_keys(self.password) # 定位登陆按钮 loadup_button = driver.find_element_by_xpath("//span[@class='ladda-label']") loadup_button.click() time.sleep() Leslie_ChanId = Loadup("176******","*******") #在此输入自己的帐号和密码 Leslie_ChanId.login()
运行程序:
可以看到成功登陆,仔细看,会发现“Chrome正在受自动测试软件的控制”,说明是selenium正在操作浏览器。期间,人工也是可以操作浏览器的。
后言:
在测试的过程中,可能会出现博客园的滑动图片验证,可能是因为频繁操作引起,以我目前所学很难实现自动滑正确的图片,所以需要人为的去滑动图片。
使用driver定位元素的方法不止有xpath,还可以通过id,name,linkt_text等等
使用selenium进入网页后,可以做很多操作,是一种更方便于爬取动态网页的方法。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- selenium基于python定位163邮箱实现自动登录
- Python selenium实现微博自动登录的示例代码
- [置顶] Webdriver(selenium2)基于Python脚本实现登录填写日志(分步讲解)
- selenium和Python3.6实现招聘狗网站自动识别验证码登录、列表页、详情页爬取
- Python使用selenium实现网页用户名 密码 验证码自动登录功能
- python + selenium 实现自动登录网站
- python+selenium实现京东自动登录及秒杀功能
- SharePoint基于windows验证的如何通过组策略实现IE自动以当前域账号登录SP站点
- python+selenium实现登录账户后自动点击的示例
- Python Selenium实现自动登录163邮箱
- selenium2+python_用cookie实现自动登录(百度首页为例)
- 基于python3+selenium实现自动枪火车票
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
- Python爬虫 Selenium实现自动登录163邮箱和Locating Elements介绍
- Python实现Selenium实现自动登录CSDN
- Selenium+phantomJS+Python 操作cookie实现自动登录(以音悦台为例)
- Python使用selenium实现网页用户名 密码 验证码自动登录功能
- selenium+python实现自动登录脚本
- Python+selenium+frame实现自动登录163邮箱