1 #-*-coding:utf-8-*-2 3 fromSeleniumImportWebdriver4 fromSelenium.webdriver.common.byImport by5 fromSelenium.webdriver.common.keysImportKeys6 fromSelenium.webdriver.support.uiImportSelect7 fromSelenium.common.exceptionsImportnosuchelementexception8 fromSelenium.common.exceptionsImportnoalertpresentexception9 ImportUnitTestTen Import Time One ImportRe A ImportHttplib - - the classXiaomi (UnitTest. TestCase): - defsetUp (self): -Self.driver =Webdriver. Chrome () -Self.base_url ='http://item.mi.com/' +Self.verificationerrors = [] -Self.accept_next_alert =True + A deflogin (): at """ - User Login - """ -Driver =Webdriver. Chrome () -Driver.get ("http://item.mi.com/product/10000041.html") -Driver.find_element_by_xpath (".//*[@id = ' j_userinfo ']/a[1]"). Click () inTime.sleep (2) -Driver.find_element_by_xpath (".//*[@id = ' username ')"). Clear () toDriver.find_element_by_xpath (".//*[@id = ' username ')"). Send_keys ('User name') +Driver.find_element_by_xpath (".//*[@id = ' pwd ']"). Clear () -Driver.find_element_by_xpath (".//*[@id = ' pwd ']"). Send_keys ('Password') theDIRVER.FIND_ELEMENT_BY_ID ('Login-button'). Click () *Assertequal (U'Login Failed', u'1253785032', Driver.find_element_by_xpath (".//*[@id = ' j_userinfo ']/span[1]/a/span"). Span) $ Panax Notoginseng - defget_xiaomi6 (self): the " " + set 9:59:55 Start browser impersonation user behavior keep clicking Add to Cart A " " the defgetsystime (): +Sys_time =time.time () - returnSys_time $ $ defSet_stamp (): -Set_time ='2017-05-12 09:59:55' #set the snapping time, preferably a few seconds ahead - #convert it to a time array theTimearray = Time.strptime (Set_time,'%y-%m-%d%h:%m:%s') - #Convert to timestampWuyiTimeStamp =Int (time.mktime (timearray)) the returnTimeStamp - Wu ifGetsystime () >=Set_stamp (): - whileTrue: AboutSelf.driver.find_element_by_class_name ('btn btn-primary btn-biglarge j_probuybtn Add'). Click () $ - ifSelf.asserttrue (U'you're late.', driver.title) = =True: - Print 'again tragedy, silently greeting millet ~' - Break A + defis_element_present (self): the Try: Self.driver.find_element (By=how, value=What ) - exceptNosuchelementexception as E:returnFalse $ returnTrue the the defis_alert_present (self): the Try: Self.driver.switch_to_alert () the exceptNoalertpresentexception as E:returnFalse - returnTrue in the defClose_alert_and_get_its_text (self): the Try: AboutAlert =Self.driver.switch_to_alert () theAlert_text =Alert.text the ifSelf.accept_next_alert: the alert.accept () + Else: - Alert.dismiss () the returnAlert_textBayi finally: theSelf.accept_next_alert =True the - defTearDown (self): - Pass the #self.driver.quit () the #self.assertequal ([], self.verificationerrors) the the if __name__=='__main__': -Unittest.main ()
Python grab Xiaomi 6 automation script