The automatic transmission 163 Mail Selenium .py

Version One:

Time Import 
Import datetime
from the Selenium Import webdriver
from selenium.webdriver.support.wait Import WebDriverWait # wait for a page to load some elements
from selenium.webdriver.support Import expected_conditions AS EC
from selenium.webdriver.common.by By Import
from getpass Import getpass

DEF the Login (the user, pwd):
"" "Log 163 E-mail" ""
# Because you can scan codes to sign, and we choose a username and password, so the password to click
the time.sleep (1)
wait.until (EC.presence_of_element_located ((By.ID, 'switchAccountLogin'))). the Click ()
# to enter the iframe, because there are multiple iframe, so get is an array, after analyzing the page, iframe 0 array is indexed landing iframes.
the time.sleep ( . 3)
iframes = driver.find_elements_by_tag_name ( 'iframes')
# Print (iframes)
' ''
[
<selenium.webdriver.remote.webelement.WebElement (the session = "3f92dbd96e72746e7d27d64e6b412318", Element = "0.855888743369456-2")>,
<selenium.webdriver.remote.webelement.WebElement (the session = "3f92dbd96e72746e7d27d64e6b412318", Element = "0.855888743369456-3 ")>,
<selenium.webdriver.remote.webelement.WebElement (the session =" 3f92dbd96e72746e7d27d64e6b412318 ", Element =" 0.855888743369456-4 ")>,
<selenium.webdriver.remote.webelement.WebElement (the session =" 3f92dbd96e72746e7d27d64e6b412318 ", = Element "0.855888743369456-5")>
]
'' '
driver.switch_to.frame (iframes [0])
# tags acquired user name and password, and enter a value corresponding to
time.sleep(1)
driver.find_element_by_class_name('dlemail').send_keys(user)
time.sleep(2)
driver.find_element_by_class_name ( 'dlpwd') send_keys (pwd).
the time.sleep (2)
driver.find_element_by_id ( 'doLogin') the Click ().

DEF the send_mail ():
"" "Send messages 163, 163 need to pass username and password , recipient and content "" "
the try:
# step 1, the implementation of landing
the login (the User, pwd)

# step 2 click write button
wait.until (EC.presence_of_element_located ((By.ID, ' _mail_component_24_24') )). the Click ()
# driver.find_element_by_id ( '_ mail_component_24_24'). the Click ()

# step 3., obtain the recipient, subject, content box label, write to
the time.sleep (1)

# 3.1 fill in the recipient
wait.until (EC.presence_of_element_located ((By.CLASS_NAME, ' nui-editableAddr-ipt'))).send_keys (addr) # recipients
the time.sleep (2)

# 3.2 Fill theme
= driver.find_elements_by_class_name title ( 'IPT-INPUT-NUI')
# Print (11111, title)
title [2] .send_keys (Theme) # topic
# title.send_keys (theme) # topic

# 3.3 where the content into the iframe, fill in the content
the time.sleep (. 1)
content_iframe = driver.find_element_by_class_name ( 'the APP-Editor-iframe')
driver.switch_to.frame (content_iframe)
# nui-scroll though a plurality of the class name in the whole page, but this is only an iframe , we directly send_keys line
nui_scroll = wait.until (EC.presence_of_element_located ((By.CLASS_NAME, 'Scroll-NUI')))
# Print (22222222, nui_scroll) # <selenium.webdriver.remote.webelement.WebElement (the session = "106a6f5778c14568827014435ddcfcd9", element = "0.07847410617283446-1 ")>
nui_scroll.send_keys (Content)

# 4 the first step, because the Send button at this time is not an iframe, so the first exit iframe, to click the send button
# 4.1 Exit iframe
the time.sleep (1)
driver.switch_to.default_content ()

# 4.2 click the send button
time.sleep (1)
the class name the # button to send more than, what is best for cycling, because there are pit, the send button is the third, and two in front of an empty tag, but seen in the front check less
driver.find_elements_by_class_name ( 'NUI-BTN-text') [2] .click ()
the finally:
# close the browser
the time.sleep (. 3)
driver.quit ()
# 2019-6-11 turned off, the code is correct

if __name__ == '__main__':
# = the INPUT the User ( "mailbox:") .strip () # write your account number 163
# pwd = getpass ( 'password:') # 163 fill in your password
user = "your mail accounts 163 "
    pwd = "your mail password 163" 

# Get Driver
Driver = webdriver.Chrome ()
the wait = WebDriverWait (Driver, 10)
# driver.maximize_window ()

# retransmission request
driver.get ( 'https://mail.163.com / ')
addr = "[email protected]" # recipients
theme =' I am your father '# topic
content =' Gang Li Chun day not angry with me, such as kendo eternal night ---- \ n {} '. format (datetime.datetime.now ()) # sending content
send_mail ()

Edition:

import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

try:
driver.get('https://mail.163.com/')
driver.maximize_window()
time.sleep(2)
wait.until(EC.presence_of_element_located((By.ID, 'switchAccountLogin'))).click()
time.sleep(1)
iframe_obj = driver.find_elements_by_tag_name('iframe')
# print(iframe_obj)
driver.switch_to.frame(iframe_obj[0])
driver.find_element_by_class_name('dlemail').send_keys('你的163邮箱账号')
time.sleep(1)
driver.find_element_by_class_name('dlpwd').send_keys('你的163邮箱密码')
time.sleep(1)
driver.find_element_by_id('dologin').click()
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="_mail_component_24_24"]/span[2]'))).click()
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'nui-editableAddr-ipt'))).send_keys('[email protected]')
theme = driver.find_elements_by_class_name('nui-ipt-input')

# print(theme)
theme[2].send_keys('邮件主题')
content = driver.find_element_by_class_name('APP-editor-iframe')
driver.switch_to.frame(content)
. driver.find_element_by_class_name ( 'NUI-the Scroll') send_keys ( 'xxxxxxxxxxxxxxx')
driver.switch_to.default_content ()

driver.find_element_by_class_name ( 'NUI-ico-Sent') the Click ().

a finally: # In any case, you must close the browser is
the time.sleep (10)
driver.quit ()

Guess you like

Origin www.cnblogs.com/zhang-da/p/12232688.html