Amanulla SK
Amanulla SK

Reputation: 1

How to take a screenshot using python - selenium?

here is my simple code of python that I wrote to save the screenshot of a webpage.

from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get("https://www.google.co.in")
driver.implicitly_wait(2)
driver.save_screenshot("D\amanulla\test.png")
driver.quit()

Though, the program running without any errors, I do not see any screenshots saved on my machine. Can someone help me on this?

Upvotes: 1

Views: 105

Answers (2)

ewwink
ewwink

Reputation: 19164

You are missing : from "D\amanulla\test.png" and you need to escape the \ as well, so effectively the line will be either:

"D:\\amanulla\\test.png"

or

"D:/amanulla/test.png"

Upvotes: 3

Corey Goldberg
Corey Goldberg

Reputation: 60644

I do not see any screenshots saved on my machine

Look for a file named Dmanulla est.png in the default Downloads location for your browser... because that is what you are instructing WebDriver to do with the line:

driver.save_screenshot("D\amanulla\test.png")

Explanation:

The string "D\amanulla\test.png" will be interpreted as "Dmanulla est.png". This is because backslashes are escape sequences within Python strings. Your directory separators will be interpreted as \a (bell) and \t (tab).

Also, the : separator is missing between the drive letter and the file path, so it is treating the entire string as a filename. In the absence of a directory name, it should save to browser's default "Downloads" directory.

Solution:

driver.save_screenshot(r"D:\amanulla\test.png")

This uses a raw string so the backslashes are not interpreted as escape sequences, and it adds the missing : as the drive letter separator.

Upvotes: 1

Related Questions