selenium 4.11版本的selenium无需在本地下载浏览器驱动
import smtplib
from email. mime. multipart import MIMEMultipart
from email. mime. text import MIMEText
from email. mime. image import MIMEImage
import base64from selenium import webdriver
from selenium. webdriver. chrome. options import Options
from selenium. webdriver. chrome. service import Servicechrome_options = Options( )
chrome_options. add_argument( '--headless' )
chrome_options. add_argument( '--disable-gpu' ) path = r'D:\chrome_driver'
service = Service( executable_path= path) def download ( url, pngName) : browser = webdriver. Chrome( options= chrome_options) try : browser. get( url) width = browser. execute_script( "return document.documentElement.scrollWidth" ) height = browser. execute_script( "return document.documentElement.scrollHeight" ) browser. set_window_size( width, height) browser. save_screenshot( pngName) browser. quit( ) except Exception as err: print ( err) browser. quit( ) finally : browser. quit( ) def send_email ( smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path) : msg = MIMEMultipart( 'related' ) msg[ 'From' ] = from_addrmsg[ 'To' ] = to_addrmsg[ 'Subject' ] = subjecthtml_content = f"""<html><body><p> { body} </p><img src="cid:image1" alt="Embedded Image"></body></html>""" msg. attach( MIMEText( html_content, 'html' ) ) with open ( image_path, 'rb' ) as img_file: img_data = img_file. read( ) image = MIMEImage( img_data, name= image_path. split( '/' ) [ - 1 ] ) image. add_header( 'Content-ID' , '<image1>' ) msg. attach( image) try : server = smtplib. SMTP( smtp_server, port) server. send_message( msg) print ( "Email sent successfully" ) except Exception as e: print ( f"Failed to send email: { e} " ) finally : server. quit( ) smtp_server = 'SMTp.test.com'
port = 25
username = 'xxxxx'
password = 'xxxxx'
from_addr = 'Test@test.com' if __name__ == '__main__' : url = 'https://console.cn/platform' pngPath = 'D:/chrome_driver/' pngName = pngPath + "summary.png" download( url, pngName) to_addr = 'test@test.com' subject = 'Here is your embedded image' body = 'Please see the image below.' image_path = pngNamesend_email( smtp_server, port, username, password, from_addr, to_addr, subject, body, image_path)