from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
class Mail:
def __init__(self,driver):
self.driver=driver
def open(self):
self.driver.get("http://www.126.com")
self.driver.maximize_window()
def click_switchAccountLogin(self):
try:
self.driver.find_element_by_id("switchAccounLogin").click() ###点击密码登录
except:
pass
def quit(self):
self.driver.quit()
def login(self,username,password):
'''
登录
:param username:
:param password:
:return:
'''
login_frame = self.driver.find_element_by_css_selector('iframe[id^="x-URS-iframe"]')
self.driver.switch_to.frame(login_frame)
self.driver.find_element_by_name("email").clear()
self.driver.find_element_by_name("email").send_keys(username)
self.driver.find_element_by_name("password").clear()
self.driver.find_element_by_name("password").send_keys(password)
self.driver.find_element_by_id("dologin").click()
self.driver.switch_to.default_content()
def logout(self):
'''
退出
:return:
'''
self.driver.find_element_by_link_text('退出').click()
def new_contact(self,name1,mail):
'''
新建联系人
:param name:
:param mail:
:return:
'''
self.click_address_book()
time.sleep(1)
self.click_new_contact()
time.sleep(1)
self.input_name(name1)
time.sleep(1)
self.input_mail(mail)
self.click_confire_contact()
def delete_contact(self,name2=None):
'''
删除联系人
:param name:
:return:
'''
self.click_address_book()
time.sleep(2)
self.move_mouse_to_contact(name2)
time.sleep(2)
self.driver.find_element_by_xpath("//div[@title='删除']/span").click()
time.sleep(2)
self.click_confire_contact()
def set_star_contact(self,name3=None):
'''
设置星标联系人
:param name:
:return:
'''
self.close_pop_up_windows()
time.sleep(2)
self.move_mouse_to_contact(name3)
time.sleep(2)
element=self.driver.find_element_by_xpath("//tr[contains(@aria-label,'"+name3+"')]/td[3]/div[2]/span[1]/b")
element.click()
def concel_star_contact(self,name4=None):
'''
取消星标联系人
:return:
'''
try:
self.move_mouse_to_contact(name4)
except:
pass
time.sleep(2)
s = WebDriverWait(driver, 5,0.5).until(EC.element_to_be_clickable((By.XPATH,"//tr[contains(@aria-label,'"+name4+"')]/td[3]/div[2]/span[1]/b")))
s.click()
time.sleep(2)
self.driver.find_element_by_xpath("//span[text()='确 定']").click()
def move_mouse_to_contact(self,name5=None):
'''
将鼠标移动到想要操作的联系人上
:param name:
:return:
'''
action = ActionChains(driver)
above = self.driver.find_element_by_xpath("//tr[contains(@aria-label,'"+name5+"')]/td[3]/div/b")
action.move_to_element(above).perform()
def close_pop_up_windows(self):
'''
关闭联系人信息的弹窗
:return:
'''
try:
self.driver.find_element_by_xpath("/html/body/div[5]/div/div[2]/div[2]/div[2]/span").click()
except:
pass
def click_address_book(self):
'''
点击通讯录
:return:
'''
self.driver.find_element_by_xpath("//*[@id='_mail_tabitem_1_4text']").click()
def click_new_contact(self):
'''
点击新建联系人
:return:
'''
self.driver.find_element_by_xpath("//span[text()='新建联系人']").click()
def input_name(self,name=None):
'''
输入新建联系人的姓名
:param name:
:return:
'''
self.driver.find_element_by_xpath("//*[@id='input_N']").send_keys(name)
def input_mail(self,mail):
'''
输入新建联系人的邮箱
:param mail:
:return:
'''
self.driver.find_element_by_xpath("//dd[@class='is0']/div[contains(@id,'_mail_input_6')]/input[1]").send_keys(mail)
def click_confire_contact(self):
'''
点击确定,保存联系人
:return:
'''
self.driver.find_element_by_xpath("//span[text()='确 定']").click()
if __name__== "__main__":
username="账号"
password="密码"=
driver=webdriver.Chrome()
driver.implicitly_wait(10)
mail=Mail(driver)
mail.open()
mail.click_switchAccountLogin()
time.sleep(1)
mail.login(username,password)
time.sleep(1)
mail.new_contact("姓名","邮箱")
time.sleep(2)
mail.set_star_contact("姓名")
time.sleep(2)
mail.concel_star_contact("姓名")
time.sleep(2)
mail.delete_contact("姓名")
time.sleep(2)
mail.logout()
mail.quit()
python+selenium3模拟126邮箱新增删除联系人,设置/取消星标联系人
最新推荐文章于 2023-09-27 18:09:17 发布