After learning selenium, we can write a script for automatic login. Take the login to the educational administration system as an example.
The first step is still to determine the library to be used
Install the library first, and enter pip install selenium on the command line. For details, please refer to this article Problems encountered in pip installation library_Lorrey_的博客-CSDN博客
Then you can import
from selenium import webdriver #Import webdriver definition from module selenium import time #Used to sleep, in order to successfully enter the page before proceeding to the next step
The second step is to download the browser driver, download the corresponding driver according to the version number of your browser
Download tutorial refer to my article chromedriver driver download_Lorrey_'s Blog-CSDN Blog
The third step, you can come to think about it, first get the url
browser.get('https://yaltest.ntvu.edu.cn/http/webvpn652e6e7476752e6564752e636e/new/index.html') #Here we take the educational administration system as an example, and friends can change the url by themselves
Enter the page, right-click to check, find the required elements, login requires user name, password, and login button
To find page elements, we can use id or xpath to find
You can quickly locate the page element by clicking here. Here we can find that the id is username, so we can write the code like this
elem_user=browser.find_element_by_id('username')
Then enter the account number by typing send_keys()
#You can write a new sentence elem_user.send_keys('your account') #You can also add directly after the previous sentence elem_user=browser.find_element_by_id('username').send_keys('your account')
Using the same method we can find the passwd element
elem_pass=browser.find_element_by_id('password').send_keys('your password')
But when we tried to find the login button in the same way, we found that the id was not found
At this time, another method can be used - xpath
Right click·Copy·XPATH
At this time, our sentence will have to change a little bit.
elem_login=browser.find_element_by_xpath('//*[@id="casLoginForm"]/p[5]/button') #The above is by_id, here is by_xpath
Then we want to simulate a mouse click, we can use click()
elem_login.click() # can also be added directly behind elem_login=browser.find_element_by_xpath('//*[@id="casLoginForm"]/p[5]/button').click()
In this way, the basic operation is realized, and then the code can be edited
from selenium import webdriver
import time
browser=webdriver.Chrome('chromedriver.exe')
#登录
browser.get('http://e.ntvu.edu.cn/new/index.html') #这里url可自行更改
#窗口最大化
browser.maximize_window()
#休息3秒,等成功打开页面再进行下一步,以免错误
time.sleep(3)
#由于我的url进去要先点击一个登录按钮才进入账号密码输入页面,具体以你输入的url实际情况为准
first_login=browser.find_element_by_class_name('amp-no-login-zh').click()
time.sleep(3)
#用户名
elem_user=browser.find_element_by_id('username')
#清空输入
elem_user.clear()
#键入用户名
elem_user.send_keys('你的账号')
time.sleep(1)
#密码
elem_pass=browser.find_element_by_id('password')
#清空输入
elem_pass.clear()
#键入用户名
elem_pass.send_keys('你的密码')
time.sleep(1)
elem_login=browser.find_element_by_xpath('//*[@id="casLoginForm"]/p[5]/button')
#点击登录按钮
elem_login.click()
exit(0)
#以上查询元素用到的id,xpath都要以你输入的url实际情况为准,方法上文我已讲了
Here is another addition. Some webpages will enter a new webpage after clicking again, that is, a new url. At this time, you will definitely not be able to find it on the previous url, so you can add these two in the code. sentence
#After clicking, a new page will be generated, so we need to transfer the handle to the new page. new= browser.window_handles browser.switch_to.window(new[-1])
Below I will post the code of my educational administration system as an example, for reference only, learn from the above
# -- coding: utf-8 --
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
browser=webdriver.Chrome('chromedriver.exe')
#登录
browser.get('https://ee.ntvu.edu.cn')
browser.maximize_window()
time.sleep(3)
first_login=browser.find_element_by_class_name('amp-no-login-zh').click()
time.sleep(3)
elem_user=browser.find_element_by_id('username')
#清空输入
elem_user.clear()
#键入用户名
elem_user.send_keys('***********')
time.sleep(1)
elem_pass=browser.find_element_by_id('password')
#清空输入
elem_pass.clear()
#键入用户名
elem_pass.send_keys('******')
time.sleep(1)
elem_login=browser.find_element_by_xpath('//*[@id="casLoginForm"]/p[5]/button')
#点击登录按钮
elem_login.click()
time.sleep(5)
#教务系统(正确的)
#点击后会生成一个新的页面,所以我们要把句柄转移到新的页面中。
new= browser.window_handles
browser.switch_to.window(new[-1])
elem_login2=browser.find_element_by_xpath('//*[@id="ampTabContentItem0"]/div[1]/pc-card-html-5856488943106402-01/amp-w-frame/div/div[2]/table/tbody/tr[1]/td[2]/a').click()
time.sleep(3)
#正方
wnd2= browser.window_handles
browser.switch_to.window(wnd2[-1])
username=browser.find_element_by_xpath('//*[@id="TextBox1"]').send_keys('***********')
time.sleep(1)
password=browser.find_element_by_xpath('//*[@id="TextBox2"]').send_keys('******')
time.sleep(1)
button=browser.find_element_by_id('RadioButtonList1_2').click()
time.sleep(1)
login=browser.find_element_by_xpath('//*[@id="Button1"]').click()
exit(0)