python + selenium + webdriver automation case QQ mailbox sends e-mail automatically. Beginners will read it once

python + selenium + webdriver automation case QQ Email automatically sends email

The overall code is at the end, the download link is also at the end, and the functions used are summarized at the top of the overall code
Let's start with step analysis:

  1. Log in to qq mailbox
  2. Enter the letter writing function
  3. Enter the recipient's email name and the content of the letter (difficult)
    Let's take the first step

Start: log in to qq mailbox

Login mailbox detailed steps

  1. Import module
  2. Create automation objects
  3. Visit website
  4. Enter account password
  5. Sign in
    In the web source code, we can see that it uses the frame embedded structure (so you need to locate the iframe tag and jump to the frame)
    Pay attention to entering your own account password
# Import the module first
from selenium import webdriver
from time import sleep
# Here, the sleep function in the time module is imported because the page will be loaded later

web = webdriver.Chrome()    # Create automation objects

url = "https://mail.qq.com/" 	# Set the URL (web address) to access
web.get(url)                    # Access browser


frame = web.find_element_by_id("login_frame")       # Positioning frame embedded labels
web.switch_to.frame(frame)                          # Switch to frame embedded label

web.find_element_by_id("u").send_keys("username") # Locate the account location and enter the account
web.find_element_by_id("p").send_keys("password") # Locate the password location and enter the password
web.find_element_by_id("login_button").click()      # Locate the login location and click login
sleep(2)

web.switch_to.parent_frame()    # Exit frame embedded label
sleep(5)        # According to the waiting time set by the network, time is used for beginners sleep()

Start: enter the writing function

Enter the detailed steps of letter writing function

  1. Navigate to the window after jump
  2. Click write
window = web.window_handles     # Locate to the jump page. The page generally needs to be relocated after jump
web.switch_to.window(window[-1])    # Locating the last page directly is generally a newly opened page. You can also use the method of not opening a new page

sleep(2)    # wait for
web.find_element_by_id("composebtn").click()    # Click write to enter the write interface

sleep(5)    # Wait for the page to load

Start: enter the recipient's mail name and the contents of the message

Here comes the technical difficulty
The source code of Lao Ma here is a little confused. It's hard to find the source code if you don't pay attention to it twice
The location of web page elements is also a difficulty. You need to find it yourself by comparing the xpath path with the source code

window = web.window_handles                     # This is the case of non new page opening. Locate the new page
web.switch_to.window(window[-1])                # location
frame = web.find_element_by_id("mainFrame")     # Position frame embedded label
web.switch_to.frame(frame)                      # Switch to frame embedded label

# The following are the location of the recipient's email name and send after the writing position_ Keys is the write content
# Note here: the writing position needs to be clicked once to start writing, and there is also a separate frame embedded label
# Recipient location
web.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys("1104774861@qq.com")
# Mail name
web.find_element_by_id("subject").send_keys("hallo world")
# Write location here, call the location directly at the switch function location, omitting frame = web find_ element_ by_ xpath('//*[@class="qmEditorIfrmEditArea"]')
web.switch_to.frame(web.find_element_by_xpath('//*[@class="qmEditorIfrmEditArea"]'))
web.find_element_by_xpath('/html/body').click()
web.find_element_by_xpath('/html/body').send_keys("How do you do!!!")

web.switch_to.parent_frame()    # Switch out of the frame of the writing position
web.find_element_by_xpath('//*[@name="sendbtn"]').click() # click the send button

sleep(5)            # TBD ensure successful sending
web.close()         # Close web page

I'll summarize the functions used here: it's more useful to read this patiently
The order is random

  1. sleep(). Wait function, fill in the number in (), and the code will automatically wait for the same time as the number when executing here Unit second
  2. Chrome() creates automation object functions to create automation functions. I use Google Chrome browser here, so chrome also has other browser corresponding functions webdriver.Chrome() requires a variable to accept a return value
  3. The get access function, like get in requests, is used to access web pages. This function does not need to pass in data such as headers, but only needs to pass in the URL address and use webdriver Chrome(). Get (URL) or (automation object) get(url)
  4. find_element_by_id() is a search function used to find the corresponding tag. It also has several similar functions, such as find_element_by_xpath() and so on are used to find tag usage (automation object) find_element_by_id()
  5. switch_to is simply understood as a jump tag used with other functions, such as web switch_to. Frame (frame) or web switch_to. window(window[-1])
  6. frame() is a function used to jump to the frame embedded tag
  7. **window() * * function used to jump to the page
  8. parent_frame() function to jump out of the frame tag
  9. window_ The return value of the handles function is a list used to get the page, so the page is often used with the window function

Total code

# @Time: 2021/8/1 1:54
# @Author: Shi Zhenfei
# @File: 02 QQ mailbox sends information automatically py
# Software: PyCharm
from selenium import webdriver
from time import sleep

web = webdriver.Chrome()    # Create automation objects

url = "https://mail.qq.com / "# set access address
web.get(url)                    # Access browser

frame = web.find_element_by_id("login_frame")       # Positioning frame embedded labels
web.switch_to.frame(frame)                          # Switch to frame embedded label

web.find_element_by_id("u").send_keys("2919390584") # Locate the account location and enter the account
web.find_element_by_id("p").send_keys("lishuyi123") # Locate the password location and enter the password
web.find_element_by_id("login_button").click()      # Locate the login location and click login
sleep(2)

web.switch_to.parent_frame()    # Exit frame embedded label
sleep(5)        # According to the waiting time set by the network, time is used for beginners sleep()

window = web.window_handles     # Locate to the jump page. The page generally needs to be relocated after jump
web.switch_to.window(window[-1])    # Locating the last page directly is generally a newly opened page. You can also use the method of not opening a new page

sleep(2)    # wait for
web.find_element_by_id("composebtn").click()    # Click write to enter the write interface

sleep(5)    # Wait for the page to load

window = web.window_handles                     # This is the case of non new page opening. Locate the new page
web.switch_to.window(window[-1])                # location
frame = web.find_element_by_id("mainFrame")     # Position frame embedded label
web.switch_to.frame(frame)                      # Switch to frame embedded label

# The following are the location of the recipient's email name and send after the writing position_ Keys is the write content
# Note here: the writing position needs to be clicked once to start writing, and there is also a separate frame embedded label
# Recipient location
web.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys("newname@qq.com")
# Mail name
web.find_element_by_id("subject").send_keys("hallo world")
# Write location here, call the location directly at the switch function location, omitting frame = web find_ element_ by_ xpath('//*[@class="qmEditorIfrmEditArea"]')
web.switch_to.frame(web.find_element_by_xpath('//*[@class="qmEditorIfrmEditArea"]'))
web.find_element_by_xpath('/html/body').click()
web.find_element_by_xpath('/html/body').send_keys("How do you do!!!")

web.switch_to.parent_frame()    # Switch out of the frame of the writing position
web.find_element_by_xpath('//*[@name="sendbtn"]').click() # click the send button

sleep(5)            # TBD ensure successful sending
web.close()         # Close web page

Download and connect domestic gitee: gitee 02-qq Email automatically sends messages py file download
Download foreign github: GitHub 02 QQ Email automatically sends messages py file download

Keywords: Python Web Development Selenium

Added by Kyrillos on Sun, 02 Jan 2022 12:00:48 +0200