python webdriver 显示等待-自动登录126邮箱,添加联系人

脚本内容:

#encoding=utf-8
#author-夏晓旭
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.common.exceptions import 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='邮箱帐号或手机号' and @name='email']"))
name.send_keys('xiaxiaoxu1987')
password=wait.until(lambda x:x.find_element_by_xpath("//input[@placeholder='密码']"))
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()会被加删除线,out了
time.sleep(5)
assert u"退出" 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_link.click()
#assert u"新建联系人" in driver.page_source
add_contact_button=wait.until(lambda x:x.find_element_by_xpath("//span[text()='新建联系人']"))
add_contact_button.click()
contact_name=wait.until(lambda x:x.find_element_by_xpath("//a[@title='编辑详细姓名']/preceding-sibling::div/input"))
contact_name.send_keys(u"徐凤钗")
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()='设为星标联系人']/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[.='确 定']"))
contact_save_button.click()


except TimeoutException, e:
# 捕获TimeoutException异常
print traceback.print_exc()

except NoSuchElementException, e:
# 捕获NoSuchElementException异常
print traceback.print_exc()

except Exception, e:
# 捕获其他异常
print traceback.print_exc()



结果:

C:\Python27\python.exe D:/test/dataDrivenTestPractice1/TestScript.py

Process finished with exit code 0


猜你喜欢

转载自www.cnblogs.com/xiaxiaoxu/p/9280721.html
今日推荐