In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use Python+Selenium to read NetEase mailbox verification code", the content is simple and clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Python+Selenium to read NetEase mailbox verification code" this article.
In the automation work, you may encounter some functions similar to sending mailbox CAPTCHA, as follows
Our general solution is:
Send email-> Open mailbox-> enter mailbox account password-> log in mailbox-> Open unread email-> get CAPTCHA-> Save CAPTCHA-> read CAPTCHA
The following is a code to open NetEase's mailbox to read unread mail and get the CAPTCHA.
Def wangyi (self,username, password, name): dr = webdriver.Edge () # Open another browser dr.maximize_window () # window maximize dr.get ("https://mail.163.com/") # Open QQ Mail time.sleep (2) iframe = dr.find_element (by=" xpath " Value= "/ html/body/div [2] / div [3] / div [1] / div/div [4] / div [1] / div [1] / iframe") dr.switch_to.frame (iframe) # switch to embedded page time.sleep (0.5) dr.find_element (by= "name" Value= "email") .clear () # clear content time.sleep (0.5) dr.find_element (by= "name", value= "email"). Send_keys (username) # enter the account time.sleep (0.5) dr.find_element (by= "name", value= "password"). Clear () # clear content time.sleep (0.5) dr.find_element (by= "name" Value= "password") .send_keys (password) time.sleep (0.5) dr.find_element (by= "id", value= "dologin") .click () # enter the mailbox homepage dr.switch_to.parent_frame () # switch back to the parent page time.sleep (2) num = dr.find_element (By.CLASS_NAME "gWel-mailInfo-status") .text # get the number of unread messages if int (num) > 0: print ("there are unread messages") dr.find_element (By.ID, "gWel-animMailIcon") .click () # Click unread messages time.sleep (2) count = dr.find_elements (By.CLASS_NAME "da0") print (len (count)) # gets the number of unread messages that exist from the third da0 to for i in range (1, len (count)): emailname = dr.find_elements (By.CLASS_NAME) "da0") [I] .text # traverses the unread email name print (emailname) print (name) if emailname = = name: # if the message name is the account name you want to match, perform the next action dr.find_element (By.CLASS_NAME 'da0') .click () # Click the email to view details time.sleep (1) iframe = dr.find_element (By.XPATH "/ html/body/div [2] / div [1] / div [3] / div/div [1] / div [6] / div/iframe") # # locate the embedded page dr.switch_to.frame (iframe) # switch to the embedded page Time.sleep (1) res = dr.find_element (By.CLASS_NAME 'netease_mail_readhtml.netease_mail_readhtml_webmail') .text # get the whole message print (res) dr.switch_to.parent_frame () # switch back to the parent interface self.code = re.findall (pattern= "\ d +") String=res) # use regular expression to get mailbox verification code print (self.code) break # get complete exit traversal else: print ('no unread mail') # if not found print not found # dr.quit () # close browser
Open the mailbox, we will find that logging in this small section is an embedded page, so the general positioning methods will fail to locate, can not click on the element, or can not find the error of the element, this is we will use dr.switch_to.frame (iframe) to switch to the embedded page method, when we switch to the embedded page, we can locate the elements in the embedded page. But if you want to operate outside the embedded page after the operation is complete, you need to use dr.switch_to.parent_frame () to change the focus back to the parent interface.
After entering the mailbox, you must first judge whether you have received the email, so you can judge whether you have received the email by the corner mark in the upper right corner of the unread email. If you receive it, you will proceed to the next step, and if you do not receive it, you will be prompted that you did not receive the email. Of course, you can also optimize it, set a cycle, check it regularly, and report an error for how long it takes.
Here, the text method is used to obtain the corner label text for judgment. Of course, the obtained text is in str format, so you need to use the INT method to convert to an integer to judge.
Num = dr.find_element (By.CLASS_NAME, "gWel-mailInfo-status"). Text # get the number of unread messages if int (num) > 0: print ("there are unread messages") else: print ("unreceived messages")
After judging that there is an unread email, we click on the unread email to check the unread email information. First of all, we need to get the unread email text information for traversal comparison. Clicking in, we can find that the properties of the email can be matched and located using Class_name, but the same Class_name has the number of emails + 1. After the study, it is found that the location of the first email is the second. So using traversal for i in range (1, len (count)), starting with the second element, count in len (count) refers to that element, and len (count) returns the number of that element, that is, the number of messages + 1
Implementation code:
Count = dr.find_elements (By.CLASS_NAME, "da0") print (len (count)) # get the number of unread messages that exist from the third da0 to for i in range (1, len (count)): emailname = dr.find_elements (By.CLASS_NAME) "da0") [I] .text # traverses the unread email name print (emailname) print (name) if emailname = = name: # if the message name is the account name you want to match, perform the next action print ("find mail to perform next") else: Print (message not found)
After finding the email, you need to get the text information. You can observe that the text message is relatively simple, in which only the CAPTCHA is a number, so you can easily match the numbers using simple regular matching. It is worth noting that the text content is also an embedded page, so you also need to switch focus.
Specific implementation code:
Dr.find_element (By.CLASS_NAME, 'da0'). Click () # Click the email to view details time.sleep (1) iframe = dr.find_element (By.XPATH) "/ html/body/div [2] / div [1] / div [3] / div/div [1] / div [6] / div/iframe") # locate the embedded page dr.switch_to.frame (iframe) # switch to the embedded page time.sleep (1) res = dr.find_element (By.CLASS_NAME 'netease_mail_readhtml.netease_mail_readhtml_webmail') .text # get the whole message print (res) dr.switch_to.parent_frame () # switch back to the parent interface self.code = re.findall (pattern= "\ d +", string=res) # use regular expressions to obtain mailbox verification codes
Return the result:
Because the returned result is a list, you need to use code [0] to retrieve it for subsequent use.
Full function code:
Import reimport timefrom selenium import webdriverfrom selenium.webdriver.common.by import By "" use Selenium to change password through CAPTCHA-- NetEase mailbox gets CAPTCHA from unread email change_pwd method: password login password newpassword new password url: website address wangyi method: username: mailbox account password: mailbox password name: email name you expect to match "" class Get_Code: def change_pwd (self,password,newpassword) " Url): driver = webdriver.Chrome () # Open browser driver.get (url) driver.maximize_window () # window maximize time.sleep (0.5) driver.find_element (by=By.XPATH Value= "/ html/body/div/div/div [1] / div/div [2] / div [3] / div [1]). Click () # Click to log in to time.sleep (1) driver.find_element (by=By.ID, value=" horizontal_login_userAccount "). Send_keys (" selenium3366@163.com ") # enter the login account driver.find_element (by=By.ID Value= "horizontal_login_password") .send_keys (password) # enter the login password driver.find_element (by=By.CLASS_NAME, value= "atn-btn-orange.ant-btn.ant-btn-lg.ant-btn-block"). Click () # Click to log in to time.sleep (2) driver.find_element (by=By.XPATH Value= "" / / * [@ id= "app"] / div/div [1] / div/div [2] / div [3] / div [1] "") .click () # Click the avatar to enter the personal center time.sleep (2) driver.find_element (by=By.XPATH Value= "" / / * [@ id= "app"] / div/div [2] / div [1] / div [2] / div [3] "") .click () # Click change_password time.sleep (1) driver.find_element (by=By.XPATH Value= "" / * [@ id= "horizontal_login_newPassword"] "") .send_keys (newpassword) # enter the new password driver.find_element (by=By.XPATH) Value= "" / * [@ id= "app"] / div/div/div [2] / form/div [2] / div/div/span/div/div [2] / a "") .click () # Click send CAPTCHA time.sleep (8) # wait for CAPTCHA to be sent to the mailbox self.wangyi (username= "selenium3366", password= "* Name= "okmarket account Information change") # call the method to get the verification code driver.find_element (by=By.XPATH) Value= "/ html/body/div [1] / div/div/div [2] / form/div [2] / div/div/span/div/div [1] / input") .send_keys (self.code [0]) # enter the verification code time.sleep (0.5) driver.find_element (by=By.XPATH Value= "" / html/body/div [1] / div/div/div [2] / form/div [3] / div/div/span/button "") .click () # Click the button time.sleep (1) text = driver.find_element (by=By.XPATH Value= "" / html/body/div [2] / span/div/div/div/span "") .text print (text) try: assert text = = "password update success" # asserts print ("password changed successfully") except: print ("modification failed") def wangyi (self,username, password) Name): dr = webdriver.Edge () # Open another browser dr.maximize_window () # window maximize dr.get ("https://mail.163.com/") # Open QQ Mail time.sleep (2) iframe = dr.find_element (by=" xpath ") Value= "/ html/body/div [2] / div [3] / div [1] / div/div [4] / div [1] / div [1] / iframe") dr.switch_to.frame (iframe) # switch to embedded page time.sleep (0.5) dr.find_element (by= "name" Value= "email") .clear () # clear content time.sleep (0.5) dr.find_element (by= "name", value= "email"). Send_keys (username) # enter the account time.sleep (0.5) dr.find_element (by= "name", value= "password"). Clear () # clear content time.sleep (0.5) dr.find_element (by= "name" Value= "password") .send_keys (password) time.sleep (0.5) dr.find_element (by= "id", value= "dologin") .click () # enter the mailbox homepage dr.switch_to.parent_frame () # switch back to the parent page time.sleep (2) num = dr.find_element (By.CLASS_NAME "gWel-mailInfo-status") .text # get the number of unread messages if int (num) > 0: print ("there are unread messages") dr.find_element (By.ID, "gWel-animMailIcon") .click () # Click unread messages time.sleep (2) count = dr.find_elements (By.CLASS_NAME "da0") print (len (count)) # gets the number of unread messages that exist from the third da0 to for i in range (1, len (count)): emailname = dr.find_elements (By.CLASS_NAME) "da0") [I] .text # traverses the unread email name print (emailname) print (name) if emailname = = name: # if the message name is the account name you want to match, perform the next action dr.find_element (By.CLASS_NAME 'da0') .click () # Click the email to view details time.sleep (1) iframe = dr.find_element (By.XPATH "/ html/body/div [2] / div [1] / div [3] / div/div [1] / div [6] / div/iframe") # # locate the embedded page dr.switch_to.frame (iframe) # switch to the embedded page Time.sleep (1) res = dr.find_element (By.CLASS_NAME 'netease_mail_readhtml.netease_mail_readhtml_webmail') .text # get the whole message print (res) dr.switch_to.parent_frame () # switch back to the parent interface self.code = re.findall (pattern= "\ d +") String=res) # use regular expression to get mailbox verification code print (self.code) break # get complete exit traversal else: print ('no unread mail') # if not found print not found # dr.quit () # close browser if _ _ name_ _ ='_ _ main__': a = Get_Code () a.change_pwd (password= "a123456" Newpassword= "a1234567", url= "*")
Full effect demonstration:
The above is "how to use Python+Selenium to read NetEase mailbox verification code" all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.