126郵箱新建一個聯繫人自動化腳本

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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章