Python Selenium 实现Edge浏览器自动登录163邮箱

📅 2026/7/14 14:34:25 👁️ 阅读次数 📝 编程学习
Python Selenium 实现Edge浏览器自动登录163邮箱

摘要:本文详细介绍了如何使用 Python Selenium 库实现 163 邮箱的自动化登录。主要内容包括:1) 安装 Selenium 库和 Edge 浏览器驱动工具;2) 导入必要的 Python 库;3) 编写自动化登录函数,包含页面加载、iframe 切换、表单填写和登录点击等完整流程;4) 运行函数的方法;5) 提供完整的可运行源码。文章提供了详细的代码示例和操作步骤,帮助读者快速掌握 Selenium 自动化测试技术在邮箱登录场景中的应用。

今天,咱们将通过 Python Selenium 库实现 163 邮箱的自动登录,以下为具体操作过程,若有不足之处,还请多多指教。

1. 安装所需的库及 Edge 浏览器驱动工具

安装 selenium 库:

pip install selenium

下载 Edge 浏览器驱动工具:

Edge 浏览器驱动工具官网:Edge 浏览器驱动工具下载官网

注:本教程所用的 Edge 浏览器及其驱动工具版本为:133.0.3065.59(正式版本)(64 位)

2. 导入所需的库

为了让浏览器有充分的响应时间,本次还将导入 time 库,导入源码如下:

import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.edge.service import Service from selenium.webdriver.common.by import By

3. 自动化登录操作代码编写

注:以下函数所需传入的参数如下:

user_name → xxxxx@163.com 中的 xxxxx

password → 指登录 163 邮箱时的密码

def login_163_mail(user_name, password): browser = webdriver.Edge() browser.get("http://mail.163.com") time.sleep(10) iframe = browser.find_element(By.XPATH, "//iframe[contains(@src, 'https://dl.reg.163.com/webzj/v1.0.1/pub/index_dl2_new.html')]") browser.switch_to.frame(iframe) # //div[@class='example'] email_name = browser.find_element(By.XPATH, "//input[@name='email']") email_name.send_keys(str(user_name)) email_password = browser.find_element(By.XPATH, "//input[@name='password']") email_password.send_keys(str(password)) login_btn = browser.find_element(By.ID, "dologin") login_btn.click() time.sleep(10)

4. 运行函数

login_163_mail(<user_name>, <password>)

5. 完整源码分享

import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.edge.service import Service from selenium.webdriver.common.by import By def login_163_mail(user_name, password): browser = webdriver.Edge() browser.get("http://mail.163.com") time.sleep(10) iframe = browser.find_element(By.XPATH, "//iframe[contains(@src, 'https://dl.reg.163.com/webzj/v1.0.1/pub/index_dl2_new.html')]") browser.switch_to.frame(iframe) # //div[@class='example'] email_name = browser.find_element(By.XPATH, "//input[@name='email']") email_name.send_keys(str(user_name)) email_password = browser.find_element(By.XPATH, "//input[@name='password']") email_password.send_keys(str(password)) login_btn = browser.find_element(By.ID, "dologin") login_btn.click() time.sleep(10) login_163_mail(<user_name>, <password>)

注:请在函数运行时把参数替换成自己的 163 邮箱用户名和密码!