I am using the python3.63 version here will talk about the installation of the problems encountered here is based on the Firefox browser to operate
1, first install Selenium in Windows CMD window through PIP did not install Pip's small partners to search the relevant Pip installation method
pip install selenium
After the installation is successful, test whether it will run correctly open Baidu input keyword and search
from selenium import webdriverimport timeDrivers=webdriver.Firefox()#声明火狐浏览器驱动对象try: Drivers.get(‘https://www.baidu.com‘) #发送get请求 input = Drivers.find_element_by_id(‘kw‘) # 找到搜索输入框 input.send_keys("小米") #输入搜索关键词 Drivers.find_element_by_id(‘su‘).click() # 点击搜索按钮 time.sleep(10) #10秒后浏览器自动关闭finally: Drivers.close()#关闭浏览器
The following error occurred while running the code
This is because there is no Firefox drive object then we go to download a Firefox driver geckodriver
This is the version of the corresponding win7 system that you download
Https://github.com/mozilla/geckodriver/releases
I'm 64, I'm on the following version.
After decompression, put the geckodriver.exe into the Python installation directory is installed in the D:/python is placed in the D:/python directory can be
Then run the code again just to be successful here to make sure your Firefox browser is the latest version if there should be no error message
There's a robot avatar next to the browser, which means the program is under surveillance, the program is installed and the test is successful. Automatic login operation
We take login 51cto as an example
from selenium import webdriverimport timeDrivers=webdriver.Firefox()#声明火狐浏览器驱动对象try: Drivers.get(‘http://home.51cto.com/index/‘) #发送get请求 input = Drivers.find_element_by_id(‘loginform-username‘) #通过元素ID找到用户名输入框 print("输入账户") input.send_keys("xxxxxx") # 输入账户 input = Drivers.find_element_by_id(‘loginform-password‘) # 通过元素ID找到密码输入框 print("输入密码") input.send_keys("xxxxxxx") # 输入密码 print("点击登录") Drivers.find_element_by_class_name(‘loginbtn‘).click() # 点击登录按钮 print("登录成功") time.sleep(10) #10秒后浏览器自动关闭finally: Drivers.close()#关闭浏览器
Log in successfully after successful login need some action to do according to their own needs
Some knowledge about the positioning of the elements we have Baidu's own brain to complement the element positioning needs you have some HTML knowledge this is very simple people take a little time to learn a good 88
Windows7 python3.63 uses Selenium+webdriver to implement automatic login using process