CA
Crawlee & Apify•17mo ago
fascinating-indigo

Taking a screenshot with Selenium/Python

Hello team! I'm just starting out and would appreciate some help here. I'm running into an error that I don't have enough tools to understand unless I see the page that's being scraped visually, so I wanted to use the save_screenshot feature with Selenium. It seems like the code does take the screenshot, and according to my understanding from guides I read, it should be saved to the KeyValueStoarge, but after I run the code, I can't find it in the storage. What am I missing? This is the piece of code I'm running on an Actor in the Apify platform:
def screenshot_error(driver, store_id):
random_number = random.randint(1000000,9999999)
key = f'ERROR-LOGIN-{random_number}'
driver.save_screenshot(f"{key}.png")
screenshot_link = f'https://api.apify.com/v2/key-value-stores/{store_id}/records/{key}'
Actor.log.info(f"screenshot path: {screenshot_link}")
def screenshot_error(driver, store_id):
random_number = random.randint(1000000,9999999)
key = f'ERROR-LOGIN-{random_number}'
driver.save_screenshot(f"{key}.png")
screenshot_link = f'https://api.apify.com/v2/key-value-stores/{store_id}/records/{key}'
Actor.log.info(f"screenshot path: {screenshot_link}")
Thanks in advance 🙂
7 Replies
Alexey Udovydchenko
Alexey Udovydchenko•17mo ago
Are you running in Apify cloud or locally? If locally, are you using Apify CLI?
fascinating-indigo
fascinating-indigoOP•16mo ago
Hi Alexey! Thank you for the reply 🙂 Sorry I wasn't clear. I'm running this code inside an Actor in the Apify platform. Does anyone here have any ideas/suggestions?
harsh-harlequin
harsh-harlequin•16mo ago
Hi @HillaShx, could you please provide the complete code sample so we can reproduce it?
fascinating-indigo
fascinating-indigoOP•16mo ago
Hi @Vlada Dusek! Thank you for your reply 🙂 Attached, the full context.
Mantisus
Mantisus•16mo ago
Hi. Your code does not save the data in key-value, your code, just saves the png files in the folder where the code is called from. You can use.
tmp = driver.get_screenshot_as_png()
tmp = driver.get_screenshot_as_png()
Then save the data to a key-value
key_store = await Actor.open_key_value_store(name='debug')
tmp = driver.get_screenshot_as_png()
await key_store.set_value(f'ERROR-LOGIN-{random_number}', tmp)
key_store = await Actor.open_key_value_store(name='debug')
tmp = driver.get_screenshot_as_png()
await key_store.set_value(f'ERROR-LOGIN-{random_number}', tmp)
fascinating-indigo
fascinating-indigoOP•16mo ago
Yeah, it made sense to me that something is missing... Now it clicks for me! Thank you @Mantisus
Mantisus
Mantisus•16mo ago
Glad it helped )

Did you find this page helpful?