Use the Python selenium-webdriver Demo Blog Park Login

Source: Internet
Author: User

beginner Python, recently in the Python+selenium automated book with the Worm master, also try to write a login function verification 2333

Code
###########################using Python selenium-webdriver#simulate the login process of the blog Park#Home--Login page--Jump back Page--check--Exit Login Recovery Environment########################### fromSeleniumImportWebdriverImport TimeImportSysurl_cnblog="https://www.cnblogs.com/"username="Chen"passwd="passwd4test" #fake, hehe ~#Check URLdefCheck_url (target_url):#print ("Target_url =", Target_url) #Get current URLCurrent_url =Browser.current_urlifCurrent_url = =Target_url:Print("Check URL Success,current_url =", Current_url)Else: Print("Check URL failed,target_url =%s,but current_url =%s"%(Target_url,current_url)) Sys.exit (1)#Check page titledefCheck_title (target_title):#print ("Target_title =", Target_title)Current_title =Browser.titleifCurrent_title = =Target_title:Print("Check title Success,current_title =", Current_title)Else: Print("Check title Failed,target_title =%s,but current_title =%s"%(target_title,current_title)) Browser=Webdriver. Firefox () Browser.maximize_window ()#Maximize Test windowbrowser.get (Url_cnblog)#Get Blog HomeTime.sleep (3) Browser.find_element_by_xpath (".//*[@id = ' span_userinfo ']/a[1]"). Click ()#Click the Home login buttonTime.sleep (2)#Login ProcessingBROWSER.FIND_ELEMENT_BY_ID ("INPUT1"). Clear () #默认可能会有提示之类文字, first empty a browser.find_element_by_id ("INPUT1"). SendKeys (username)#Time.sleep (2)browser.find_element_by_id ("Input2"). Clear () browser.find_element_by_id ("Input2"). SendKeys (passwd) browser.find_element_by_id ("signin"). Click () time.sleep (3)#Check if you are logged in and jump homeCheck_url (Target_url)#Check page titleCheck_title ("Blog Park-Developer's online home")#Gets the current login user name can also be optimized to function ~Current_User = Browser.find_element_by_xpath (".//*[] @id = ' span_userinfo '/a[1]"). Text#the. Text method to get the text of the specified element#print ("Current_User =", Current_User)ifCurrent_User = =Username:Print("Check login username Success,current_user =", Current_User)Else: Print("Check login username failed,username =%s,but current_user ="%(username,current_user))#Log out to restore the environmentBrowser.find_element_by_xpath (".//*[] @id = ' span_userinfo '/a[5]"). Click ()Print("Alert_text =", Browser.switch_to_alert (). Text)#Confirm pop-up window processingBrowser.switch_to_alert (). Accept () Time.sleep (2) Browser.quit ()


Use the Python selenium-webdriver Demo Blog Park Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.