[英]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.这是我编写的用于保存网页屏幕截图的简单 python 代码。
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?有人可以帮我吗?
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"
并且您还需要转义\\
,因此该行将是:
"D:\\amanulla\\test.png"
or或者
"D:/amanulla/test.png"
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:在浏览器的默认下载位置查找名为Dmanulla est.png
的文件...因为这是您指示 WebDriver 对以下行执行的操作:
driver.save_screenshot("D\\amanulla\\test.png")
Explanation:解释:
The string "D\\amanulla\\test.png"
will be interpreted as "Dmanulla est.png"
.字符串"D\\amanulla\\test.png"
将被解释为"Dmanulla est.png"
。 This is because backslashes are escape sequences within Python strings.这是因为反斜杠是 Python 字符串中的转义序列。 Your directory separators will be interpreted as \\a
(bell) and \\t
(tab).您的目录分隔符将被解释为\\a
(bell) 和\\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.这使用原始字符串,因此反斜杠不会被解释为转义序列,并且它添加了缺少的:
作为驱动器号分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.