from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time, traceback
driver = None
wait = None
#登录函数
def login_126():
global driver
global wait
driver = webdriver.Chrome(executable_path="d:\\chromedriver")
url = "http://www.126.com"
driver.get(url)
# 定义显式等待对象
wait = WebDriverWait(driver, 10, 0.2)
try:
# 切入登录框所在的iframe
wait.until(EC.frame_to_be_available_and_switch_to_it(( \
By.XPATH, '//iframe[contains(@id,"x-URS-iframe")]')))
time.sleep(3)
# driver.switch_to.frame(driver.find_element_by_xpath(\
# '//iframe[contains(@id,"x-URS-iframe")]'))
# 显式等待用户名输入框可见且获取输入框元素对象
username = wait.until(lambda x: x.find_element_by_xpath( \
'//input[@placeholder="邮箱帐号或手机号"]'))
username.send_keys("xxx")
# 直接获取密码输入框
passwd = driver.find_element_by_xpath('//input[@name="password"]')
passwd.send_keys("xxxxx")
# 获取登录按钮并点击
driver.find_element(By.ID, "dologin").click()
#且出到正文
driver.switch_to.default_content()
# 显式等待登录成功后“退出”按钮出现在页面上
if wait.until(EC.visibility_of_element_located(('xpath', '//a[.="退出"]'))):
print("登录成功!")
else:
print("登录失败!")
except NoSuchElementException as e:
print(traceback.print_exc())
except TimeoutException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
#退出驱动并关闭所有浏览器窗口
def close_driver():
global driver
driver.quit()
#添加联系人
def add_contact():
global driver
global wait
try:
#显式等待“通讯录”按钮可见,且获取元素
address_book = wait.until(EC.element_to_be_clickable(("xpath",'//div[.="通讯录"]')))
address_book.click()
#显式等待新建联系人按钮可见,且获取按钮元素
new_contact = wait.until(lambda x : x.find_element_by_xpath('//span[.="新建联系人"]'))
#wait.until(EC.element_to_clickable('xpath','//span[.="新建联系人"]'))
new_contact.click()
#显式等待新建联系人页面的“姓名”输入框出现,并获取元素对象
nameBox = wait.until(EC.visibility_of_element_located(('id','input_N')))
nameBox.send_keys("hhhhhh")
#分别获取邮箱、手机号等其他元素对象
emailBox = driver.find_element_by_xpath('//div[@id="iaddress_MAIL_wrap"]//input')
emailBox.send_keys("[email protected]")
#获取设置星标联系人复选框
starBox = driver.find_element_by_xpath(\
'//span[.="设为星标联系人" and @class="nui-chk-text"]//preceding-sibling::*/b')
starBox.click()
phoneBox = driver.find_element_by_xpath(\
'//div[@id="iaddress_TEL_wrap"]//input')
phoneBox.send_keys("13122222222")
commentBox = driver.find_element_by_id("input_DETAIL")
commentBox.send_keys("hhhh")
#点击确定按钮
driver.find_element_by_xpath('//span[.="确 定"]').click()
#利用新增联系人后生成的页面元素定位
#emailStr = "[email protected]"
if wait.until(EC.visibility_of_element_located(('xpath','//nobr[.="[email protected]"]'))):
print("新增联系人成功!")
else:
print("新增联系人失败!")
except NoSuchElementException as e:
print(traceback.print_exc())
except TimeoutException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
def main():
global driver
global wait
login_126()
add_contact()
close_driver()
if __name__ == "__main__":
main()
126邮箱新建一个联系人自动化脚本
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.