如何修复 Python Selenium 脚本中的“innerHTML”错误?

问题描述 投票:0回答:1

我在使用 Selenium 库使用 Python 开发的 gmail 发送脚本中遇到错误,我需要帮助来修复此错误。错误详情如下:

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\New folder\gmailsender.py", line 101, in <module>
    sb.driver.execute_script(f'document.querySelector(\'div[role="textbox"]\').innerHTML = `{body_content}`;')
  ...
selenium.common.exceptions.JavascriptException: Message: javascript error: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.

我收到错误的代码行:

sb.driver.execute_script(f'document.querySelector(\'div[role="textbox"]\').innerHTML = 
{body_content}
;')

脚本工作原理:将template.html文件中的html代码粘贴到邮件发送部分。

我编写了一个从gmail发送邮件的脚本,之前运行得很顺利,但是由于gmail的新更新,我认为我的脚本不再工作。我该如何解决这个错误?错误的完整版本:

"Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\New folder\gmailsender.py", line 101, in <module>
    sb.driver.execute_script(f'document.querySelector(\'div[role="textbox"]\').innerHTML = `{body_content}`;')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 407, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.
  (Session info: chrome=122.0.6261.111)
Stacktrace:
        GetHandleVerifier [0x00007FF72267AD22+56930]
        (No symbol) [0x00007FF7225EF622]
        (No symbol) [0x00007FF7224A42E5]
        (No symbol) [0x00007FF7224A9261]
        (No symbol) [0x00007FF7224AB9FD]
        (No symbol) [0x00007FF722528E57]
        (No symbol) [0x00007FF72250BC9A]
        (No symbol) [0x00007FF7225281E2]
        (No symbol) [0x00007FF72250BA43]
        (No symbol) [0x00007FF7224DD438]
        (No symbol) [0x00007FF7224DE4D1]
        GetHandleVerifier [0x00007FF7229F6AAD+3709933]
        GetHandleVerifier [0x00007FF722A4FFED+4075821]
        GetHandleVerifier [0x00007FF722A4817F+4043455]
        GetHandleVerifier [0x00007FF722719756+706710]
        (No symbol) [0x00007FF7225FB8FF]
        (No symbol) [0x00007FF7225F6AE4]
        (No symbol) [0x00007FF7225F6C3C]
        (No symbol) [0x00007FF7225E68F4]
        BaseThreadInitThunk [0x00007FF8B4BE4ED0+16]
        RtlUserThreadStart [0x00007FF8B5ACE39B+43]
"



python html selenium-webdriver selenium-chromedriver innerhtml
1个回答
0
投票

如果您正在创建 gmail 发送脚本,那么最好使用专门用于发送邮件的库。这里我使用

smtplib
(简单邮件传输协议):

import smtplib

s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login('[email protected]', 'mysecretpassword1234')
message = 'message'
s.sendmail('[email protected]', 'receiver email address', message)
s.quit()

当然,如果你想让电子邮件有更多的功能,有主题和正文,你可以试试这个:

import smtplib
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart   

s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login('[email protected]', 'mysecretpassword1234')
msg = MIMEMultipart()
body = 'your message body'
msg['Subject'] = 'your subject for the email'
msg.attach(MIMEText(body))
email = msg
s.sendmail('[email protected]', 'receiver email address', msg=email)
s.quit()
© www.soinside.com 2019 - 2024. All rights reserved.