I've been trying to get an automatic login program, and today I've been thinking about it for a while. But the function is not much. But for me, it's also a small improvement. The knowledge accumulates, the nature will be more. Needless to say, the code is the most practical.
Environment:
- System: WINDOWS10
- IDE Development tools: Pycharm Professional
- Module: Selenium,time
Install Selinium:
pip install selenium
Browser-driven configuration:
Firefox driver: geckodriver
1. 相关链接: GitHub:[github地址](https://github.com/mozilla/geckodriver) :[ffirefoxdriver下载](https://github.com/mozilla/geckodriver/releases)根据自己平台和Firefox的版本来下载Firefox的驱动版本。不知道Firefox的版本和驱动版本是否对应,可以自行百度。windows下, 解压geckodriver.zip, 将exe文件放到python的Scripts目录下。linux下: 可以设置环境变量。验证: 可以直接在cmd/linux终端敲geckodriver命令:有geckodriver的正常输出说明OK。
Chrome Driver: Chromedriver
Official website: https://sites.google.com/a/chromium.org/chromedriver
: https://chromedriver.storage.googleapis.com/index.html
Check out the version of Chrome and then download the chrome driver for your Chrome browser.
The rest of the steps are consistent with Firefox steps. Do not make a detailed explanation.
Pycharm hack website: pycharm hack
The code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-import timefrom Selenium Import Webdriverfrom Selenium.webdriver.common.keys Import keysdef Login (): Try:driver = Webdriver. Firefox () url = "https://im.qq.com/index.shtml" driver.get (URL) Text = driver.find_element_by_id (' Top Bar '). Text driver.find_element_by_id ("login"). Click () Time.sleep (5) # Open the QQ login screen, ready to log in DRIVER.SWI Tch_to_frame (' Login_frame ') driver.find_element_by_id ("Switcher_plogin"). Click () time.sleep (5) #填写QQ账 Number driver.find_element_by_id (' U '). Send_keys (' QQ number ') Time.sleep (5) #填写QQ密码: Driver.find_element_b y_id (' P '). Send_keys (' password ') #模拟点击登录按钮, execute login driver.find_element_by_id (' Login_button '). Click () #退出浏览器, exit QQ, you can set your own sleep time as needed. Time.sleep (+) Driver.quit () except:print ("program error") Print ("Please check the script") if __name__== ' __main__ ': logi N ()
Python3 automatic Login to Web page QQ