qq mailbox login-mail for selenium

1. Browser opens QQ mailbox login web address

QQ mailbox login address is: https://mail.qq.com/

driver = webdriver.Chrome()

driver.get('https://mail.qq.com/')

2. Element Positioning, Enter QQ Account and QQ Password

Manually enter the QQ mailbox login page, press option+command+i to open the developer tools, click View Elements, select elements:

You can see that the QQ account input box id, name and other attributes are good choices that can be directly used to locate.

Here I choose the most common xpath method to locate, id='u', and enter a QQ account with the send event

(You can also use driver.find_element_by_id('u') to locate it, the code is clearer, but I'm personally used to using the generic xpath)

driver.find_element_by_xpath('//*[@id="u"]').send_keys("XXXXXXXXX")

Similarly, the QQ password element attribute id='p', the same positioning method

driver.find_element_by_xpath('//*[@id="p"]').send_keys("XXXXXXXXX")

Similarly, the login button element attribute id='login_button', you don't need to send information here, so select click click event

driver.find_element_by_xpath('//*[@id="login_button"]').click()

Okay, so far in principle, running pycharm should be able to enter an account and password and log in successfully.

But it's not that simple. After running, selenium reported an error and couldn't find the element with id u.

Looking carefully, I see that frame nested pages are funny.

We can understand that each web page is a parent frame, and since we visited this web address, we have entered this parent frame nesting. As the name implies, a father is a son. Frame (parent) is nested with iframe (child), if the element we want to locate is in iframe, we need to switch to iframe first.

iframe also has its own element properties, and selenium also provides the switch method for us to use

Look up the iframe information again with option+command+i, you can see that it is nested by iframe, and the id and name are'login_frame'

 

Before locating an element, enter the following code:

driver.switch_to.frame("login_frame")

This switches to iframe and continues with previous element positioning to be successful.

The complete code for this step is:

# Locate login_frame

driver.switch_to.frame("login_frame")

# Locate account, password and enter

driver.find_element_by_xpath('//*[@id="u"]').send_keys("XXXXXXXXX")

driver.find_element_by_xpath('//*[@id="p"]').send_keys("XXXXXXXXX")

driver.find_element_by_xpath('//*[@id="login_button"]').click()

# Locate login button

driver.find_element_by_xpath('//*[@id="login_button"]').click()

By this time the web page has been able to successfully QQ mailbox.

3. Element Positioning, Writing Interface

Continue. After successful mailbox login, we come to the Writing Interface. As usual, we need to click the Writing button in the upper left corner to expand the specific Writing Interface first.

In the same way, option+command+i operates, looks at the Write button, locates the element as id='composebtn', and sends the click event

driver.find_element_by_xpath('//*[@id="composebtn"]').click()

Mail content editing has four parts: recipient, subject, body, and last click on send button

By the pit I stepped in before, at this point, I became very careful about iframe nesting, and each element I was ready to locate watched to see if it was nested by iframe.

Otherwise, the Recipient, Subject, and Send are nested by the mainFrame under the main document, and the Body is nested by the sub frame s of the mainFrame.

so, the logic for this step is:

(1) Switch to mainFrame first,

(2) Locate the recipient and the subject separately, call send event

(3) Continue switching to subframe

(4) Locate the body and call send event

(5) Return from the child frame to its parent frame, which is the mainFrame

(6) Locate the send button and invoke the click event

In this step, there are also many unexpected pits:

1. When locating the recipient, the located element and subdiv are found. After simulation, it is found that only the second subdiv is really the element that can locate the recipient. First locate id='toAreaCtrl', then select the input in the second div as the location.

The specific xpath locations are:

"//*[@id='toAreaCtrl']/div[2]/input"

2. The same problem with locating the recipient is that you must add a delay to the execution after switching to mainFrame and before locating the recipient, or you will not be able to locate the recipient element. The reason is unknown, so I suggest you try adding a time if you encounter an element that cannot be located later. Sleep.

3. When locating the body, switch from mainFrame to iframe and find that the id and name of the iframe are a dynamic string of numbers, but switch_to.frame only supports fixed IDs or names. So think of another way to do this, first locate the xpath with iframe's class, then pass it to switch_to.frame to switch. Specifically:

# Switch to iframe

driver.switch_to.frame(driver.find_element_by_xpath('//*[@class="qmEditorIfrmEditArea"]'))

4. The message body needs to invoke a click event to activate before the send event can be started. If you don't click and write, send's content is placed in the text box after the topic. (And I don't know why there's such a setting ~)

So to sum up, the code for this step is:

# Switch to mainFrame

driver.switch_to.frame('mainFrame')

sleep(1)

# Locate the recipient and enter

driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys("XXXXXX@qq.com")

# Locate the theme and enter

driver.find_element_by_xpath('//*[@id="subject"]').send_keys("mail from lucas")

# Locate message body, enter iframe first

driver.switch_to.frame(driver.find_element_by_xpath('//*[@class="qmEditorIfrmEditArea"]'))

# You must click on the body before sending_ Keys

driver.find_element_by_xpath('/html/body').click()

driver.find_element_by_xpath('/html/body').send_keys("Hello World", "\nlucas")

# Return to mainframe

driver.switch_to.parent_frame()
# Locate Send Button

driver.find_element_by_xpath('//*[@name="sendbtn"]').click()

Final program code:

# -*- coding: utf-8 -*-



"""

@author: lucas

@Function:

@file: sendEmail.py

@time: 2021/8/26 2:55 Afternoon

"""

from telnetlib import EC



from selenium import webdriver

from time import sleep



from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait



driver = webdriver.Chrome()

driver.get('https://mail.qq.com/')

sleep(2)

driver.maximize_window()

# Locate login_frame

driver.switch_to.frame('login_frame')

# driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click()



# Locate account, password and enter

sleep(2)

driver.find_element_by_xpath('//*[@id="u"]').send_keys("XXXXXXXXX")

sleep(2)

driver.find_element_by_xpath('//*[@id="p"]').send_keys("XXXXXXXXX")

sleep(2)

driver.find_element_by_xpath('//*[@id="login_button"]').click()

# Locate login button

driver.find_element_by_xpath('//*[@id="login_button"]').click()

sleep(2)

driver.find_element_by_xpath('//*[@id="composebtn"]').click()

# Switch to mainFrame

driver.switch_to.frame('mainFrame')

sleep(1)

# Locate the recipient and enter

driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys("XXXXXXXXX")

# Locate the theme and enter

driver.find_element_by_xpath('//*[@id="subject"]').send_keys(" from lucas")

# Locate message body, enter iframe first

driver.switch_to.frame(driver.find_element_by_xpath('//*[@class="qmEditorIfrmEditArea"]'))

# You must click on the body before sending_ Keys

driver.find_element_by_xpath('/html/body').click()

driver.find_element_by_xpath('/html/body').send_keys("Hello World", "\nlucas")

# Return to mainframe

driver.switch_to.parent_frame()

# Locate Send Button

driver.find_element_by_xpath('//*[@name="sendbtn"]').click()

sleep(5)

Last message sent successfully:

4. Summary of Element Positioning

1. frame is important. Make sure to see if it is nested and switch
2. If the element's id or name is dynamic, discard it
3. Use xpath to locate true fragrance
4. Use relative paths to continue positioning if elements have child nodes
5. We can't really figure out why the positioning failed. Try using time.sleep()

5. Tips:

Share a few more convenient tips in developer tools:

1. In the developer tool, select the element and click Console, you can see whether the element is nested by iframe intuitively

2. Mail replication can get the xpath of element positioning directly

Keywords: Python Selenium

Added by Eugene on Tue, 21 Dec 2021 16:00:09 +0200