Selenium2 Python Example
The function of the following script is: Open Google browser-"jump to a URL-" Enter the user name and password login-"read the data in the page and sum.
1 #Coding=utf-8 #编码声明2 Import Time3 fromSeleniumImportWebdriver4 5 #Jump to homepage6Driver = Webdriver. Chrome ()#Optional argument, if not specified would search path.7Driver.implicitly_wait (15)8Driver.get ('http://xxx.xxx.xxx') # URL9 #Driver.maximize_window ()TenTime.sleep (5)#Let the user actually see something! OneLoginButton1 = driver.find_element_by_id ('Login'# Locate elements by ID A Loginbutton1.click () -DRIVER.FIND_ELEMENT_BY_ID ('username'). Send_keys (' xxxxxx') # User name -DRIVER.FIND_ELEMENT_BY_ID ('Password'). Send_keys ('yyyyyy') # password theTime.sleep (5) -Driver.find_element_by_xpath ('//button[@type = "Submit"]'). Click () # position elements through XPath -Time.sleep (5)#Let the user actually see something! - + #set up a mechanism -Driver.find_element_by_xpath ("//*[@id = ' profile-messages ']/a/b"). Click () +Driver.find_element_by_xpath ("//*[@id = ' profile-messages ']/ul/li[3]/a"). Click () ATime.sleep (5) at #Work Ranking -Driver.find_element_by_xpath ("//*[@id = ' Articles_rank ']/a"). Click () - #All Media -Driver.find_element_by_xpath ("//*[@id = ' media_0 ')"). Click () - #Set the time -Driver.find_element_by_xpath ("//*[@id = ' reservation ')"). Click () inDriver.find_element_by_xpath ("//div[@class = ' Calendar left ']//th[@class = ' prev Available ']"). Click () -Driver.find_element_by_xpath ("//div[@class = ' Calendar left ']//td[@data-title= ' r0c6 ']"). Click ()#October 1 toDriver.find_element_by_xpath ("//div[@class = ' Calendar right ']//th[@class = ' prev Available ']"). Click () +Driver.find_element_by_xpath ("//div[@class = ' Calendar right ']//td[@data-title= ' r5c1 ']"). Click ()#October 31 -Driver.find_element_by_xpath ("//body/div[8]/div[1]/div/button[1]"). Click () the * #read Target and evaluate $Time.sleep (1# Add delay, resolve "Click Operation immediately after searching for elements using Element.text, quote stale element Error" issuePanax NotoginsengTotal =Int (0) -Elementlist = Driver.find_elements_by_xpath ("//*[@id = ' articlecontent ']//td[5]") the forElementinchelementlist: +Text2 =Element.text AValue =Int (text2) theTotal = Total +value +Hasnext =False - while1: $Buttonlist = Driver.find_elements_by_xpath ("//*[@id = ' page ']//a") $Pagebutton = Buttonlist[len (buttonlist)-1] - ifPagebutton.text = ="Next": -Hasnext =True theDriver.execute_script ("Arguments[0].scrollintoview ();", Pagebutton) # Executes the JS statement, scrolling through the page, moving elements that are not in the field of view to the field of view. - Pagebutton.click ()Wuyi Else: theHasnext =False - driver.quit () Wu Break - ifHasnext: AboutTime.sleep (1) $Elementlist = Driver.find_elements_by_xpath ("//*[@id = ' articlecontent ']//td[5]") - forElementinchelementlist: -Text1 =Element.text -Value =Int (Text1) ATotal = Total +value + Print "Value:"+Str (value) the Print "Total:"+str (total) - $ the # theDriver.quit ()
Selenium2 Python Example