1. Initial
SELENIUM[1] is a tool for Web application testing. The selenium test runs directly in the browser, just as the real user is doing. Supported browsers include IE (7, 8, 9, ten, one), Mozilla firefox,safari,google Chrome,opera, etc. The main features of this tool include: test and browser compatibility--test your application to see if it works well on different browsers and operating systems. Test system functions--Create regression test to verify software functionality and user requirements. Supports automatic recording of actions and automatic generation of test scripts in different languages such as. Net, Java, and Perl.
2. How to use
Case:
Demand:
The company purchased a group of Ruckus wireless APs, I have just dabbled in the Python crawler industry, the need to achieve the function is to have a good dozens of sets of wireless APs timed restart. Originally done TP-LINK941N wireless router timed restart, according to the original idea to find ways-------grab the bag. By crawling the link to restart the router, the wireless router is scheduled to restart (scheduled events are given to the operating system's own Task Scheduler). Then began to grab the bag operation, but ultimately failed to end---perhaps because I am not very skilled in the clutch kit. But the function is still to be realized, by chance to hear friends mentioned before the selenium can imitate the operation of the human, reptile operation. So I started the study of selenium.
1, the login interface of the router
2, first environment preparation.
python3.5.2
pip9.0.1
PIP3 Install Selenium
The latest version of the Google browser (FQ upgrade things will no longer boil)
Latest version of Webdriver (http://chromedriver.storage.googleapis.com/index.html)
Note: 3.31 is the latest version, you can open the folder to see the specific time of the drive.
Google Browser needs to add the environment variables, the driver files are placed under the Google Chrome installation path.
3, the first user name and password login
Import Module
from Import Webdriver
Let the program open the browser, of course you can also use other browsers, I am here to use the Google browser.
Browser = Webdriver. Chrome ()
4. Find the place to enter your user name and password.
5, copy the selector and XPath path of this tag, which can be. I'm using XPath here.
# Get user name input box login_name = Browser.find_element_by_xpath ('//*[@id = "Login-username"]) # Get input box to enter password Login_pwd = Browser.find_elements_by_xpath ('//*[@id = "password"]') [0] # Get Login button = Browser.find_elements_by_xpath ('//*[@id = " LoginForm "]/input") [0]
6, get the user name and password and click the login button, after we need to give the user name and password assignment and click the login button
# Enter user name Login_name.send_keys ('super') in the location of the user name Enter Password login_pwd.send_keys ('gaosiedu.com') in the input box of Enter password Click the login button Button.Click ()
7, Login Success We found a problem, wireless AP background page with a frame tag (involving a variety of cut, but do not have a headache to see below)
A is the left menu bar, B is the right side of the content bar, we first have to go inside a to find the left click on the Restart button, and then cut out, then go into the B frame tag to get the restart button, click Action.
#找到第一个frame标签 (left column) browser.switch_to.frame ("NavFrame") #切换进去, execute reboot now this button, Reboot_bo = Browser.find_element_by_xpath ('/html/body/dl[3]/dd/ul/li[2]/a ') #执行操作, get the restart button on the right reboot now Reboot_ Bo.click () #跳出当前的frame标签 browser.switch_to.default_content () #找到执行完后右边的frame标签 Browser.switch_ To.frame (' mainframe ') #进入标签后找到重启的按钮 reboot_boot = Browser.find_element_by_xpath ('//*[@id = "Adminform"]/ TABLE/TBODY/TR[1]/TD/INPUT[2] ') #执行重启的操作 Reboot_boot.click ()
8, the finished, if there are 60 wireless APs, there are many places to optimize. The complete code is as follows
fromSeleniumImportWebdriver fromSelenium.webdriver.support.uiImportwebdriverwaitImport TimeImportThreadingImportOsbrowser=Webdriver. Chrome ()#Load IP ListdefIp (): With open ('Ip_list.txt','R') as file: forIpinchfile.readlines (): IP=Ip.strip () reboot_spiders (IP)#Restart MethoddefReboot_spiders (ARG):ifLen (ARG) >0:Try: Browser.get ("https://"+ arg +"/login.asp") #Get user name input boxLogin_name = Browser.find_element_by_xpath ('//*[@id = "Login-username"]') #Get input box to enter passwordLogin_pwd = Browser.find_elements_by_xpath ('//*[@id = "password"]') [0]#Get the login buttonbutton = Browser.find_elements_by_xpath ('//*[@id = "LoginForm"]/input') [0] Time.sleep (2) #Enter the user name in the location where you entered the user nameLogin_name.send_keys ('Super') #Enter the password in the input boxLogin_pwd.send_keys ('gaosiedu.com') #Click the login buttonButton.Click ()#stop for 2 seconds .Time.sleep (2) #analyze pages that are already logged in #login successful, start analysis page! #find the first frame tag (left column)Browser.switch_to.frame ("NavFrame") #switch in, execute reboot now this button,Reboot_bo = Browser.find_element_by_xpath ('/html/body/dl[3]/dd/ul/li[2]/a') #take action to get the restart button on the right reboot nowReboot_bo.click ()#jump out of the current frame labelbrowser.switch_to.default_content ()#Find the frame label on the right after executionBrowser.switch_to.frame ('Mainframe') #after entering the label, find the restart buttonReboot_boot = Browser.find_element_by_xpath ('//*[@id = "Adminform"]/table/tbody/tr[1]/td/input[2]') #Perform a restart operationReboot_boot.click ()#Close BrowserTime.sleep (2) #Generate restart Success LogTimee = Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) S_log="time%s IP address [%s] restarted successfully"%(Timee,arg)#Write FileSuccess (S_log)exceptException as E:#Build failure error logTimeee = Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) R_log="time%s IP address [%s] Restart failed"%(Timeee,arg)#Write FileError (R_log)Pass Else: Pass#error LogdefError (args): With open ('Error.txt','A +', encoding='Utf-8') as File:file.write ('\n%s'%args) file.close ()#Success LogdefSuccess (args): With open ('Success.txt','A +', encoding='Utf-8') as File:file.write ('\n%s'%args) file.close ()if __name__=='__main__': T1= Threading. Thread (Target=ip (), args=(),) T1.start () browser.quit ( )Source Code
1, the establishment of ip_list.txt file, the IP address as below write in. 172.16.5.2172.16.5.1172.16.5.5172.16.5.50172.16.5.512, this script provides a success log and error log 3, If you need to be aware of more places on the online Windows Task Scheduler, you can email me if you need to [email protected].
Precautions
Selenium operation:
Detailed operation can see my good friend van Brother's blog, here I will not repeat.
http://www.pythonsite.com/?p=188
Python Crawler's initial selenium