如何在发生异常时截屏并通过电子邮件发送? (Python)

How to take a screenshot when except happens and send it in a email? (Python)

我想在发生异常时使用 python/selenium 截屏并通过电子邮件发送?我该怎么做呢?我不想保存它,但我不介意将它保存到文件夹中,但我想发送一封电子邮件,说明发生错误的位置。到目前为止,我所看到的所有示例似乎都将其保存到一个文件夹中。我的代码如下。

def myfunction(self):
         try:
         #some code
                
         except NoSuchElementException:
         print("error appeared")
         #take screenshot here
         subject = '')
         message = (f"""Here is the screeenshot of the error """)       
            self.sendEmailBOTS(self.errorReportemail, self.errorReportemailpasswd, self.errorReportemail, subject, message)
            for mail in self.ccBots:
                self.sendEmailBOTS(self.errorReportemail, self.errorReportemailpasswd, mail, subject, message)  


def sendEmailBOTS(self, email, password, send_to, subject, message):
        print("Sending email to", send_to)
        logging.info("Sending email to")
        msg = MIMEMultipart()
        msg["From"] = email
        msg["To"] = send_to
        msg["Subject"] = subject
        msg.attach(MIMEText(message, 'plain'))
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(email, password)
        text = msg.as_string()
        server.sendmail(email, send_to, text)
        server.quit()

你可以试试这样截图:

myfunction(self):
    try:
    # some code

    except NoSuchElementException:
        print("error appeared")
        # take screenshot here
        driver.save_screenshot('file name here')
def somefunction(self):
imagepath = os.path.join(root, 'Errorimages\ERROR')
image_screenshot = imagepath + ".png"
self.driver.save_screenshot(image_screenshot)
subject = '')
message = (f"""Here is the screeenshot of the error """)       
self.sendEmailBOTS(self.errorReportemail, self.errorReportemailpasswd, self.errorReportemail, subject, message)
for mail in self.ccBots:
  self.sendEmailBOTS(self.errorReportemail, self.errorReportemailpasswd, mail, subject, message)  


 def filefunction(self):

def sendEmailBOTS(self, email, password, send_to, subject, message):
        print("Sending email to", send_to)
        logging.info("Sending email to")
        msg = MIMEMultipart()
        msg["From"] = email
        msg["To"] = send_to
        msg["Subject"] = subject
        msg.attach(MIMEText(message, 'plain'))
        filename = "ERROR.png"
        locationImages = os.path.join(root, 'Errorimages\ERROR.png')
        attachment = open(locationImages, "rb")
        
        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

        msg.attach(part)

        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(email, password)
        text = msg.as_string()
        server.sendmail(email, send_to, text)
        server.quit()

This is what I came up with. Its not perfect but it takes the screenshot ,saves it in a folder and then the email sends that picture.