Script content:
#encoding =utf-8
#author-Xia Xiaoxu
From seleniumImport Webdriver
Import time
From Selenium.webdriver.support.uiImport webdriverwait
From selenium.webdriver.common.byImport by
From Selenium.common.exceptionsImport TimeoutException, Nosuchelementexception
Import Traceback
Driver=webdriver. Firefox (Executable_path=C\\Geckodriver ')
Driver.get (' Http://mail.126.com ')
Try
Wait=webdriverwait (Driver,10,0.2)#显示等待
Driver.switch_to.frame (Driver.find_element_by_xpath ("//iframe[@id = ' x-urs-iframe ']")#切换到用户名和密码输入框所在的frame元素
Name=wait.until (Lambda X:x.find_element_by_xpath ("//input[@placeholder = ' Email account or mobile number ' and @name = ' email ']")
Name.send_keys (' xiaxiaoxu1987 ')
Password=wait.until (Lambda X:x.find_element_by_xpath ("//input[@placeholder = ' password ']")
Password.send_keys (' Gloryroad ')
Submit=wait.until (Lambda X:x.find_element_by_xpath ("//a[@id = ' dologin ']")
Submit.click ()
Driver.switch_to.default_content ()#在pycharm里用switch_to_default_content () will be added to the delete line, out of the
Time.sleep (5)
AssertU "Exit"In Driver.page_source,"No exist in Page_source"
Address_book_link=wait.until (Lambda X:x.find_element_by_xpath ("//div[text () = ' Address Book ']")
Address_book_link.click ()
#assert u "new Contact" in Driver.page_source
Add_contact_button=wait.until (Lambda X:x.find_element_by_xpath ("//span[text () = ' new Contact ']")
Add_contact_button.click ()
Contact_name=wait.until (Lambda X:x.find_element_by_xpath ("//a[@title = ' Edit detailed name ']/preceding-sibling::d iv/input"))
Contact_name.send_keys (U "describes Chai")
Contact_email=wait.until (Lambda X:x.find_element_by_xpath ("//*[@id = ' iaddress_mail_wrap ']//input"))
Contact_email.send_keys ("[Email protected]")
Contact_is_star=wait.until (Lambda X:x.find_element_by_xpath ("//span[text () = ' Set as Star Contact ']/preceding-sibling::span/b"))
Contact_is_star.click ()
Contact_mobile=wait.until (Lambda X:x.find_element_by_xpath ("//*[@id = ' iaddress_tel_wrap ']//dd//input"))
Contact_mobile.send_keys (' 18141134488 ')
Contact_other_info=wait.until (Lambda X:x.find_element_by_xpath ("//textarea"))
Contact_other_info.send_keys (' My wife ')
Contact_save_button=wait.until (Lambda X:x.find_element_by_xpath ("//span[.= ' OK ']"))
Contact_save_button.click ()
except timeoutexception, E:
# capture TimeoutException exception
print traceback.print_exc ()
except Nosuchelementexception, E:
# catch Nosuchelementexception exception
Span style= "color: #808080; Font-style:italic "> print traceback.print_exc ()
except exception, E:
# Catch other exceptions
print traceback.print_exc ()
Results:
C:\Python27\python.exe d:/test/datadriventestpractice1/testscript.py
Process finished with exit code 0
Python webdriver display wait-Auto login 126 mailbox, add contact