lundi 18 février 2019

Selenium saves blank image of canvas

I have following canvas element being loaded.

<canvas height="522" width="810" id="chart-1" style="display: block; width: 900px; height: 580px; touch-action: none; outline: none; background: white; cursor: auto;" tabindex="8888"></canvas>

and I am trying to store it as PNG. I have tried following

    from selenium import webdriver
    import time
    import base64

    driver = webdriver.Chrome()
    driver.get("http://localhost:4200/agreements")
    table_rows = driver.find_element_by_xpath("/html/body/app-root/app-agreements/div/table/tbody/tr[1]")
    table_rows.click()

    #Works perfectly fine till here !

    time.sleep(2)
    canvas = driver.find_element_by_xpath('//*[@id="chart-1"]')

    # get the canvas as a PNG base64 string
    canvas_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", canvas)
    # decode
    canvas_png = base64.b64decode(canvas_base64)

    # save to a file
    with open(r"canvas.png", 'wb') as f:
        f.write(canvas_png)

Basically I have a chart loaded in canvas which gets loaded when user clicks a row from a table which has different data sets. and I want to see whether any changes are happening.But it saves only first image empty.

Urls before and after clicking the table :

http://localhost:4200/documents

http://localhost:4200/documents/e894de0e-349a-4b25-8fbb-68f6fce8ac06;documentType=NonDisclosureAgreement

Aucun commentaire:

Enregistrer un commentaire