selenium与python自动化测试模拟登录百度
< 返回列表时间: 2018-09-03来源:OSCHINA
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
将一些必须必要条件准备好
1,安装chorme浏览器
1,安装依赖 sudo apt-get install libxss1 libappindicator1 libindicator7
2,下载google安装包 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
3,安装 sudo dpkg -i google-chrome*.deb sudo apt-get install -f
2,安装chormedrive
1,安装xvfb以便我们可以无头奔跑地运行Chrome sudo apt-get install xvfb
2,安装依赖 sudo apt-get install unzip
3,下载安装包 wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
4,解压缩+添加执行权限 unzip chromedriver_linux64.zip
5,移动 sudo mv -f chromedriver /usr/local/share/chromedriver
6,建立软连接 sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
3,安装python依赖
1,安装Python依赖 pip3 install selenium
百度的模拟登录 ''' 百度的模拟登录,但手机的验证码只能手动输入 ''' from selenium import webdriver import time sel = webdriver.Chrome() myurl = "https://www.baidu.com/" sel.get(myurl) #找到登录节点,进行登录 try: sel.find_element_by_css_selector("#u1 a:nth-child(7)").click() print("click success!!") except: print("click failed") #js页面加载出来的页面必须要有time.sleep来取加载页面 # 等待页面加载时间 time.sleep(2) #找到用户名登录的节点,进行登录 try: sel.find_element_by_css_selector("p#TANGRAM__PSP_10__footerULoginBtn").click() print("click username login success!") except: print("not find username login!") #设置休眠时间.模拟用户点击 time.sleep(1) #查找手机号输入框,username就是自己要登录的手机帐号 try: sel.find_element_by_css_selector("#TANGRAM__PSP_10__userName").send_keys("username") print("input success!") except: print("input fail please input again") #查找密码输入框,进行输入密码,password就是对应的密码 try: sel.find_element_by_css_selector("#TANGRAM__PSP_10__password").send_keys("password") print("input password success") except: print("input password failed") #点击登录 try: sel.find_element_by_css_selector("#TANGRAM__PSP_10__submit").click() print("click success!") except: print("click failed") time.sleep(1) #点击发送验证码 try: sel.find_element_by_css_selector("#TANGRAM__36__button_send_mobile").click() print("send information to your moblephone") except: print("send information failed") time.sleep(1) #如何得到验证码并输入 #手机验证码暂时没有办法自动获取到,只能手动输入,这个问题主要是由于设置的安全登录引起的 try: info = input("输入验证码:") sel.find_element_by_css_selector("#TANGRAM__36__input_vcode").send_keys(info) print("input success") except: print("input failed") time.sleep(1) ###点击确定键 try: sel.find_element_by_css_selector("#TANGRAM__36__button_submit").click() print("click ensure success") except: print("click failed")
热门排行