Incognito UTF-8
Incognito UTF-8

Reputation: 35

Take screenshot with selenium and python

I try to make a script who can scrape Instagram.

For the moment I just open and log into insta with my script.

I would like to take a screenshot but my script close when he have to take the script and I don't have a JPG image inside my folder

This is my code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time 

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
dt = driver.get("https://www.instagram.com")
content = driver.find_element_by_css_selector('body > div.RnEpo.Yx5HN._4Yzd2 > div > div > button.aOOlW.bIiDR')
content.click()
time.sleep(10)
element_username = driver.find_element_by_name("username")
element_username.send_keys("mail")
time.sleep(5)
element_password = driver.find_element_by_name("password")
element_password.send_keys("password")
time.sleep(5)
element_clique = driver.find_element_by_css_selector('#loginForm > div.qF0y9.Igw0E.IwRSH.eGOV_._4EzTm.kEKum > div:nth-child(3) > button > div')
element_clique.click()
time.sleep(5)
element_clique_bt1 = driver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button')
element_clique_bt1.click()
time.sleep(15)
element_clique_bt2 = driver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
element_clique_bt2.click()
time.sleep(25)
element_clique_bt3 = driver.find_element_by_css_selector('.XTCLo')
element_clique_bt3.send_keys("#survivalism", Keys.ENTER)
element_clique_bt4 = driver.find_element_by_css_selector('#fcc4df1f5d15e > div > div')
element_clique_bt4.click()
driver.get_screenshot_as_file("LambdaTestVisibleScreen.png") 

time.sleep(50)

Is there something wrong with my variable driver?

Upvotes: 2

Views: 4640

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

get_screenshot_as_file(filename) saves a screenshot of the current window to a PNG image file.This method returns False if there is any IOError, else returns True.

However, it is also suggested to use full paths in the filename and instead of a JPG file you must look for a PNG file. So effectively your line of code will be:

driver.get_screenshot_as_file("/path/to/LambdaTestVisibleScreen.png") 

Reference

You can find a couple of relevant detailed discusssions in:

Upvotes: 4

Related Questions