How to Fix 'innerHTML' Error in Python Selenium Script?

48 Views Asked by At

I'm getting an error in my gmail sending script that I developed with Python using Selenium library and I need help to fix this error. The details of the error are as follows:

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.

The line of code where I got the error:
sb.driver.execute_script(f'document.querySelector(\'div[role="textbox"]\').innerHTML = {body_content};')

How the script works: It pastes the html codes in the template.html file into the mail sending section.

I coded a script to send mail from gmail, it was working smoothly before, but due to the new update to gmail, I think my script is no longer working. how can I solve this error? full version of the error:

"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]
"



1

There are 1 best solutions below

0
5rod On

If you're creating a gmail sending script, it is much better to use a library for sending mail specifically. Here I'm using smtplib (simple mail transfer protocol):

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()

Of course, if you wanted the email to have slightly more features, with a subject and body, you can try this:

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()