Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: execute_script() throws error with too large strings #13495

Closed
DevNicks opened this issue Jan 23, 2024 · 5 comments
Closed

[🐛 Bug]: execute_script() throws error with too large strings #13495

DevNicks opened this issue Jan 23, 2024 · 5 comments
Labels
C-py G-chromedriver Requires fixes in ChromeDriver I-defect

Comments

@DevNicks
Copy link

DevNicks commented Jan 23, 2024

What happened?

HTML:

<div id="div-to-make-to-pdf">
    <p>Some Text</p>
    <img src="" alt="">
</div>

The image src is set by an input type="file".
If the image is too large and the HTML code is therefore larger than 33,500,000 characters, the error occurs.
If the HTML code is smaller than 33,500,000 characters, there is no error.

JavaScript - get the HTML-Code to convert to pdf and send it to backend:

async function send_data() {

    let html_str = document.getElementById("div-to-make-to-pdf").outerHTML
    html_str = html_str.replace(/`/g, '\\`')

    await fetch("", {
        method: 'POST',
        body: JSON.stringify({
            html_str: html_str
        }),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            "X-CSRFToken": getCookie('csrftoken')
        },
    })
}

Python - make HTML to pdf:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

def pdf_erstellen(html_str):

    settings = {
        "landscape": False,
        "displayHeaderFooter": False,
        "printBackground": True,
        "marginTop": 0,
        "marginRight": 0,
        "marginBottom": 0,
        "marginLeft": 0,
        "pageRanges": "1",
        "paperWidth": 8.26,
        "paperHeight": 11.69
    }

    options = ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--kiosk-printing")

    driver = Chrome(service=Service(ChromeDriverManager().install()), options=options)
    driver.get("https://www.example.com/create-pdf/")

    script = f"document.querySelector('body').innerHTML=`{html_str}`;"
    driver.execute_script(script)

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.TAG_NAME, "body")))

    pdf_data = driver.execute_cdp_cmd("Page.printToPDF", settings)

    driver.quit()

    return(pdf_data["data"])

Exact string lengths:
Code works with 33,424,541 characters
Code fails with 33,552,149 characters

So I think the error occurs at a length of 33,500,000 characters

I don't understand why selenium gives the the error SyntaxError: Unexpected token ')' cause there's no ) in my code.

So I think it execute_script has a character limit.

Is there a way to remove this limit, or is there a work around to send a string larger that 33,500,000 characters?

How can we reproduce the issue?

If "html_str" has more than 33,500,000 characters "driver.execute_script(html_str)" will cause an error.
If "html_str" is less than 33,500,000 characters, the code works.

Relevant log output

DEBUG:selenium.webdriver.remote.remote_connection:Remote response: status=500 | data={"value":{"error":"unknown error","message":"unknown error: Runtime.callFunctionOn threw exception: SyntaxError: Unexpected token ')'\n  (Session info: chrome-headless-shell=120.0.6099.225)","stacktrace":"\tGetHandleVerifier [0x00886EE3+174339]\n\t(No symbol) [0x007B0A51]\n\t(No symbol) [0x004C6FF6]\n\t(No symbol) [0x004CAC51]\n\t(No symbol) [0x004CC7C1]\n\t(No symbol) [0x0052ACD8]\n\t(No symbol) [0x0051700C]\n\t(No symbol) [0x0052A104]\n\t(No symbol) [0x00516DA6]\n\t(No symbol) [0x004F1034]\n\t(No symbol) [0x004F1F8D]\n\tGetHandleVerifier [0x00924B1C+820540]\n\tsqlite3_dbdata_init [0x009E53EE+653550]\n\tsqlite3_dbdata_init [0x009E4E09+652041]\n\tsqlite3_dbdata_init [0x009D97CC+605388]\n\tsqlite3_dbdata_init [0x009E5D9B+656027]\n\t(No symbol) [0x007BFE6C]\n\t(No symbol) [0x007B83B8]\n\t(No symbol) [0x007B84DD]\n\t(No symbol) [0x007A5818]\n\tBaseThreadInitThunk [0x7679FCC9+25]\n\tRtlGetAppContainerNamedObjectPath [0x77CA7C6E+286]\n\tRtlGetAppContainerNamedObjectPath [0x77CA7C3E+238]\n"}} | headers=HTTPHeaderDict({'Content-Length': '1001', 'Content-Type': 'application/json; charset=utf-8', 'cache-control': 'no-cache'})
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapper_view
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\project\app_name\views.py", line 39, in app_name
    file_as_pdf = create_pdf(post_data.get("html_str"))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\project\app_name\views.py", line 232, in create_pdf
    driver.execute_script(script)
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 408, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 348, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.callFunctionOn threw exception: SyntaxError: Unexpected token ')'
  (Session info: chrome-headless-shell=120.0.6099.225)
Stacktrace:
        GetHandleVerifier [0x00886EE3+174339]
        (No symbol) [0x007B0A51]
        (No symbol) [0x004C6FF6]
        (No symbol) [0x004CAC51]
        (No symbol) [0x004CC7C1]
        (No symbol) [0x0052ACD8]
        (No symbol) [0x0051700C]
        (No symbol) [0x0052A104]
        (No symbol) [0x00516DA6]
        (No symbol) [0x004F1034]
        (No symbol) [0x004F1F8D]
        GetHandleVerifier [0x00924B1C+820540]
        sqlite3_dbdata_init [0x009E53EE+653550]
        sqlite3_dbdata_init [0x009E4E09+652041]
        sqlite3_dbdata_init [0x009D97CC+605388]
        sqlite3_dbdata_init [0x009E5D9B+656027]
        (No symbol) [0x007BFE6C]
        (No symbol) [0x007B83B8]
        (No symbol) [0x007B84DD]
        (No symbol) [0x007A5818]
        BaseThreadInitThunk [0x7679FCC9+25]
        RtlGetAppContainerNamedObjectPath [0x77CA7C6E+286]
        RtlGetAppContainerNamedObjectPath [0x77CA7C3E+238]

ERROR:django.request:Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapper_view
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\project\app_name\views.py", line 39, in app_name
    file_as_pdf = create_pdf(post_data.get("html_str"))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\project\app_name\views.py", line 232, in create_pdf
    driver.execute_script(script)
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 408, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 348, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\Documents\GitHub\django-project\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.callFunctionOn threw exception: SyntaxError: Unexpected token ')'
  (Session info: chrome-headless-shell=120.0.6099.225)
Stacktrace:
        GetHandleVerifier [0x00886EE3+174339]
        (No symbol) [0x007B0A51]
        (No symbol) [0x004C6FF6]
        (No symbol) [0x004CAC51]
        (No symbol) [0x004CC7C1]
        (No symbol) [0x0052ACD8]
        (No symbol) [0x0051700C]
        (No symbol) [0x0052A104]
        (No symbol) [0x00516DA6]
        (No symbol) [0x004F1034]
        (No symbol) [0x004F1F8D]
        GetHandleVerifier [0x00924B1C+820540]
        sqlite3_dbdata_init [0x009E53EE+653550]
        sqlite3_dbdata_init [0x009E4E09+652041]
        sqlite3_dbdata_init [0x009D97CC+605388]
        sqlite3_dbdata_init [0x009E5D9B+656027]
        (No symbol) [0x007BFE6C]
        (No symbol) [0x007B83B8]
        (No symbol) [0x007B84DD]
        (No symbol) [0x007A5818]
        BaseThreadInitThunk [0x7679FCC9+25]
        RtlGetAppContainerNamedObjectPath [0x77CA7C6E+286]
        RtlGetAppContainerNamedObjectPath [0x77CA7C3E+238]

[23/Jan/2024 14:57:31] "POST / HTTP/1.1" 500 20804

Operating System

Windows 10

Selenium version

selenium==4.16.0

What are the browser(s) and version(s) where you see this issue?

the user browser isn't relevant

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 120.0.6099.225

Are you using Selenium Grid?

No response

Copy link

@DevNicks, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@titusfortner
Copy link
Member

The question is whether Selenium is not sending the whole thing to the driver and it complains, or whether Selenium sends the whole thing to the driver and the driver can't handle it.

What happens when you run with Firefox?

@DevNicks
Copy link
Author

The question is whether Selenium is not sending the whole thing to the driver and it complains, or whether Selenium sends the whole thing to the driver and the driver can't handle it.

What happens when you run with Firefox?

I tried it with Edge and same error when executing execute_script.
But execute_script works with Firefox, so it seems like the Chrome and Edge driver can't handle it.
But Firefox doesn't display big images (in my case an image with 25MB), so I can't use Firefox to print a pdf.

Seems like I need a work around for Chrome, cause I think I can't fix the driver.

@titusfortner titusfortner added G-chromedriver Requires fixes in ChromeDriver and removed R-awaiting answer labels Jan 24, 2024
Copy link

Hi, @DevNicks.
This issue has been determined to require fixes in ChromeDriver.

You can see if the feature is passing in the Web Platform Tests.

If it is something new, please create an issue with the ChromeDriver team.
Feel free to comment the issues that you raise back in this issue. Thank you.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C-py G-chromedriver Requires fixes in ChromeDriver I-defect
Projects
None yet
Development

No branches or pull requests

2 participants