Mayi_ use selenium to automatically log in to 126/163 mailboxes and send emails

Reprinted from: https://www.cnblogs.com/yin-tao/p/7244082.html

I am using python2.7.13+selenium

ps: A few days ago, I tried to write this code many times, but it failed when I clicked the step of writing a letter, I think my problem should be roughly these points:

       1. The write button cannot be located directly, and its parent directory id is dynamic. I used xpath to locate this dynamic id at that time, so failure is inevitable, so we need to continue to look up to find the parent directory, that is, more The success rate of hierarchical xpath (it is recommended to use more xpath/css) will be higher, because the fewer the layers, the greater the possibility of repetition (for example, the class of the subject in the 126 letter is the same as the class of the search box, of course, also there are many)

       2. The loading of the page is not complete, resulting in an error when the element is not found, so I need to use display and implicit waiting

       3. At the same time, clear the cache in the browser in time, because too much garbage will cause the browser to run slowly or even crash

Well, there are a lot of words, just paste the code directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#coding:UTF-8
import  time
from  selenium.webdriver.common.keys  import  Keys
from  selenium  import  webdriver
 
driver  =  webdriver.Chrome()
driver.implicitly_wait( 30 )<br> #隐式等待,不影响用例执行速度<br>#如果登录时出现了验证码,可以将等待时长设置更长(如60),手动点击完成验证,并点击登录(ps:自动验证码很难,我感觉这种类型的验证码是其中的Boss)
driver.get( 'http://mail.126.com/' )
driver.switch_to_frame( 'x-URS-iframe' )<br> #登录页面存在iframe
driver.find_element_by_name( 'email' ).clear()
driver.find_element_by_name( 'email' ).send_keys( 'testingwtb' )
driver.find_element_by_name( 'password' ).send_keys( 'a123456' , Keys.ENTER)
time.sleep( 6 )<br> #跳转页面时,强制等待6s
driver.find_element_by_xpath( "//div[@id='dvNavTop']/ul/li[2]/span[2]" ).click()<br> #点击写信按钮
time.sleep( 2 )
driver.find_element_by_class_name( 'nui-editableAddr-ipt' ).send_keys( '[email protected]' )<br> #收件人
driver.find_element_by_xpath( "//input[@class='nui-ipt-input' and @type='text' and @maxlength='256']" ).send_keys(u '测试' )<br> #主题
xpath  =  driver.find_element_by_xpath( "//div[@class='APP-editor-edtr']/iframe" )
driver.switch_to_frame(xpath)<br> #文本内容在iframe中
driver.find_element_by_xpath( "//body[@class='nui-scroll' and @contenteditable='true']" ).send_keys(u '这是一个自动化测试邮件' )
driver.switch_to_default_content()<br> #发送按钮在iframe外,所以需要跳出
driver.find_element_by_xpath( "//div[@class='nui-toolbar-item']/div/span[2]" ).click()<br> #发送

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325358351&siteId=291194637