I have a page where an image is generated once some javascript is run. I can use splinter
to get to the point where I have the image but I am unable to save it.
The code I am using is really simple:
browser = Browser('firefox')
browser.visit(png_url)
browser.driver.save_screenshot(str(step+1) + '.png')
But the screenshot comes up empty...
The page is basically:
<body style="margin: 0px;">
<img style="-webkit-user-select: none" src="http://localhost:8000/x">
</body>
I can right click and save the image but am not sure how to automate that with splinter
.
Also, the image is an inline png, when I check the sources it reads as:
data:image/png;base64,iVBORw0KGgoAAAANSUhE
etc...
What can I do to automatically save this image? I am trying to generate a list of links and then iterate through them and save an image from each link.
Add a delay allowing the image to appear via an explicit wait:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(browser.driver, 10)
wait.until(EC.visibility_of_element_located((By.TAG_NAME, "img")))
browser.driver.save_screenshot(str(step+1) + '.png')
or, with time.sleep()
(not recommended):
import time
time.sleep(5)
browser.driver.save_screenshot(str(step+1) + '.png')
Alternatively, read that base64 image data, decode and save it (not tested):
image_data = browser.driver.find_element_by_tag_name("img").get_attribute("src")
image_data = image_data[22:] # getting rid of data:image/png;base64,
fh = open(str(step+1) + '.png', "wb")
fh.write(image_data.decode('base64'))
fh.close()