Webdriver Screenshot
When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?
python selenium selenium-webdriver webdriver
add a comment |
When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?
python selenium selenium-webdriver webdriver
add a comment |
When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?
python selenium selenium-webdriver webdriver
When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file to a specific directory?
python selenium selenium-webdriver webdriver
python selenium selenium-webdriver webdriver
edited Nov 22 '16 at 15:17
Danra
5,09814294
5,09814294
asked Jan 17 '12 at 18:55
user1152578user1152578
257158
257158
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
Use driver.save_screenshot('/path/to/file')
or driver.get_screenshot_as_file('/path/to/file')
:
import selenium.webdriver as webdriver
import contextlib
@contextlib.contextmanager
def quitting(thing):
yield thing
thing.quit()
with quitting(webdriver.Firefox()) as driver:
driver.implicitly_wait(10)
driver.get('http://www.google.com')
driver.get_screenshot_as_file('/tmp/google.png')
# driver.save_screenshot('/tmp/google.png')
1
Hi,driver.save_screenshot('/path/to/file')
works on Windows, butdriver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates<div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".
– Tommy L
Apr 20 '18 at 16:10
@TommyL:save_screenshot
callsget_screenshot_as_file
, so if one worked, so should the other.
– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
add a comment |
Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
add a comment |
Yes, we have a way to get screenshot extension of .png using python webdriver
use below code if you working in python webriver.it is very simple.
driver.save_screenshot('Dfolderfilename.png')
add a comment |
Sure it isn't actual right now but I faced this issue also and my way:
Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.
Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):
def urlify(self, s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^ws]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"s+", '-', s)
return s
then
driver.save_screenshot('c:\pytest_screenshots\%s' % screen_name)
where
def datetime_now(prefix):
symbols = str(datetime.datetime.now())
return prefix + "-" + "".join(symbols)
screen_name = self.urlify(datetime_now('screen')) + '.png'
add a comment |
This will take screenshot and place it in a directory of a chosen name.
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
add a comment |
driver.save_screenshot("path to save \screen.jpeg")
add a comment |
You can use below function for relative path as absolute path is not a good idea to add in script
Import
import sys, os
Use code as below :
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
make sure you create the folder where the .py file is present.
os.path.join
also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep
is similar like File.separtor
in java
add a comment |
Here they asked a similar question, and the answer seems more complete, I leave the source:
How to take partial screenshot with Selenium WebDriver in python?
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
add a comment |
I understand you are looking for an answer in python, but here is how one would do it in ruby..
http://watirwebdriver.com/screenshots/
If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.
eg:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8900073%2fwebdriver-screenshot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use driver.save_screenshot('/path/to/file')
or driver.get_screenshot_as_file('/path/to/file')
:
import selenium.webdriver as webdriver
import contextlib
@contextlib.contextmanager
def quitting(thing):
yield thing
thing.quit()
with quitting(webdriver.Firefox()) as driver:
driver.implicitly_wait(10)
driver.get('http://www.google.com')
driver.get_screenshot_as_file('/tmp/google.png')
# driver.save_screenshot('/tmp/google.png')
1
Hi,driver.save_screenshot('/path/to/file')
works on Windows, butdriver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates<div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".
– Tommy L
Apr 20 '18 at 16:10
@TommyL:save_screenshot
callsget_screenshot_as_file
, so if one worked, so should the other.
– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
add a comment |
Use driver.save_screenshot('/path/to/file')
or driver.get_screenshot_as_file('/path/to/file')
:
import selenium.webdriver as webdriver
import contextlib
@contextlib.contextmanager
def quitting(thing):
yield thing
thing.quit()
with quitting(webdriver.Firefox()) as driver:
driver.implicitly_wait(10)
driver.get('http://www.google.com')
driver.get_screenshot_as_file('/tmp/google.png')
# driver.save_screenshot('/tmp/google.png')
1
Hi,driver.save_screenshot('/path/to/file')
works on Windows, butdriver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates<div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".
– Tommy L
Apr 20 '18 at 16:10
@TommyL:save_screenshot
callsget_screenshot_as_file
, so if one worked, so should the other.
– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
add a comment |
Use driver.save_screenshot('/path/to/file')
or driver.get_screenshot_as_file('/path/to/file')
:
import selenium.webdriver as webdriver
import contextlib
@contextlib.contextmanager
def quitting(thing):
yield thing
thing.quit()
with quitting(webdriver.Firefox()) as driver:
driver.implicitly_wait(10)
driver.get('http://www.google.com')
driver.get_screenshot_as_file('/tmp/google.png')
# driver.save_screenshot('/tmp/google.png')
Use driver.save_screenshot('/path/to/file')
or driver.get_screenshot_as_file('/path/to/file')
:
import selenium.webdriver as webdriver
import contextlib
@contextlib.contextmanager
def quitting(thing):
yield thing
thing.quit()
with quitting(webdriver.Firefox()) as driver:
driver.implicitly_wait(10)
driver.get('http://www.google.com')
driver.get_screenshot_as_file('/tmp/google.png')
# driver.save_screenshot('/tmp/google.png')
edited Nov 4 '14 at 0:43
answered Jan 17 '12 at 19:03
unutbuunutbu
560k10512051258
560k10512051258
1
Hi,driver.save_screenshot('/path/to/file')
works on Windows, butdriver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates<div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".
– Tommy L
Apr 20 '18 at 16:10
@TommyL:save_screenshot
callsget_screenshot_as_file
, so if one worked, so should the other.
– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
add a comment |
1
Hi,driver.save_screenshot('/path/to/file')
works on Windows, butdriver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates<div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".
– Tommy L
Apr 20 '18 at 16:10
@TommyL:save_screenshot
callsget_screenshot_as_file
, so if one worked, so should the other.
– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
1
1
Hi,
driver.save_screenshot('/path/to/file')
works on Windows, but driver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".– Tommy L
Apr 20 '18 at 16:10
Hi,
driver.save_screenshot('/path/to/file')
works on Windows, but driver.get_screenshot_as_file('/path/to/file')
doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha">
or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".– Tommy L
Apr 20 '18 at 16:10
@TommyL:
save_screenshot
calls get_screenshot_as_file
, so if one worked, so should the other.– unutbu
Apr 20 '18 at 20:30
@TommyL:
save_screenshot
calls get_screenshot_as_file
, so if one worked, so should the other.– unutbu
Apr 20 '18 at 20:30
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
@TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.
– unutbu
Apr 20 '18 at 20:36
add a comment |
Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
add a comment |
Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
add a comment |
Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
edited May 23 '17 at 12:10
Community♦
11
11
answered Dec 16 '13 at 12:23
Ran AdlerRan Adler
2,2961821
2,2961821
add a comment |
add a comment |
Yes, we have a way to get screenshot extension of .png using python webdriver
use below code if you working in python webriver.it is very simple.
driver.save_screenshot('Dfolderfilename.png')
add a comment |
Yes, we have a way to get screenshot extension of .png using python webdriver
use below code if you working in python webriver.it is very simple.
driver.save_screenshot('Dfolderfilename.png')
add a comment |
Yes, we have a way to get screenshot extension of .png using python webdriver
use below code if you working in python webriver.it is very simple.
driver.save_screenshot('Dfolderfilename.png')
Yes, we have a way to get screenshot extension of .png using python webdriver
use below code if you working in python webriver.it is very simple.
driver.save_screenshot('Dfolderfilename.png')
edited Dec 13 '12 at 8:49
Sirko
54.8k15102145
54.8k15102145
answered Dec 13 '12 at 8:31
Kv.senthilkumarKv.senthilkumar
68621427
68621427
add a comment |
add a comment |
Sure it isn't actual right now but I faced this issue also and my way:
Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.
Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):
def urlify(self, s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^ws]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"s+", '-', s)
return s
then
driver.save_screenshot('c:\pytest_screenshots\%s' % screen_name)
where
def datetime_now(prefix):
symbols = str(datetime.datetime.now())
return prefix + "-" + "".join(symbols)
screen_name = self.urlify(datetime_now('screen')) + '.png'
add a comment |
Sure it isn't actual right now but I faced this issue also and my way:
Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.
Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):
def urlify(self, s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^ws]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"s+", '-', s)
return s
then
driver.save_screenshot('c:\pytest_screenshots\%s' % screen_name)
where
def datetime_now(prefix):
symbols = str(datetime.datetime.now())
return prefix + "-" + "".join(symbols)
screen_name = self.urlify(datetime_now('screen')) + '.png'
add a comment |
Sure it isn't actual right now but I faced this issue also and my way:
Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.
Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):
def urlify(self, s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^ws]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"s+", '-', s)
return s
then
driver.save_screenshot('c:\pytest_screenshots\%s' % screen_name)
where
def datetime_now(prefix):
symbols = str(datetime.datetime.now())
return prefix + "-" + "".join(symbols)
screen_name = self.urlify(datetime_now('screen')) + '.png'
Sure it isn't actual right now but I faced this issue also and my way:
Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.
Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):
def urlify(self, s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^ws]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"s+", '-', s)
return s
then
driver.save_screenshot('c:\pytest_screenshots\%s' % screen_name)
where
def datetime_now(prefix):
symbols = str(datetime.datetime.now())
return prefix + "-" + "".join(symbols)
screen_name = self.urlify(datetime_now('screen')) + '.png'
edited May 23 '17 at 12:10
Community♦
11
11
answered May 6 '15 at 10:23
Vladimir KolenovVladimir Kolenov
959
959
add a comment |
add a comment |
This will take screenshot and place it in a directory of a chosen name.
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
add a comment |
This will take screenshot and place it in a directory of a chosen name.
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
add a comment |
This will take screenshot and place it in a directory of a chosen name.
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
This will take screenshot and place it in a directory of a chosen name.
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
edited Nov 26 '18 at 4:53
hellpanderr
2,53221633
2,53221633
answered Jun 27 '18 at 14:35
Ger McGer Mc
345418
345418
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
add a comment |
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.
– sniperd
Jun 27 '18 at 15:15
add a comment |
driver.save_screenshot("path to save \screen.jpeg")
add a comment |
driver.save_screenshot("path to save \screen.jpeg")
add a comment |
driver.save_screenshot("path to save \screen.jpeg")
driver.save_screenshot("path to save \screen.jpeg")
answered May 8 '15 at 7:44
ShaikShaik
1888
1888
add a comment |
add a comment |
You can use below function for relative path as absolute path is not a good idea to add in script
Import
import sys, os
Use code as below :
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
make sure you create the folder where the .py file is present.
os.path.join
also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep
is similar like File.separtor
in java
add a comment |
You can use below function for relative path as absolute path is not a good idea to add in script
Import
import sys, os
Use code as below :
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
make sure you create the folder where the .py file is present.
os.path.join
also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep
is similar like File.separtor
in java
add a comment |
You can use below function for relative path as absolute path is not a good idea to add in script
Import
import sys, os
Use code as below :
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
make sure you create the folder where the .py file is present.
os.path.join
also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep
is similar like File.separtor
in java
You can use below function for relative path as absolute path is not a good idea to add in script
Import
import sys, os
Use code as below :
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
make sure you create the folder where the .py file is present.
os.path.join
also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep
is similar like File.separtor
in java
edited Jan 29 '18 at 12:54
answered Jan 29 '18 at 7:32
Shubham JainShubham Jain
7,92353668
7,92353668
add a comment |
add a comment |
Here they asked a similar question, and the answer seems more complete, I leave the source:
How to take partial screenshot with Selenium WebDriver in python?
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
add a comment |
Here they asked a similar question, and the answer seems more complete, I leave the source:
How to take partial screenshot with Selenium WebDriver in python?
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
add a comment |
Here they asked a similar question, and the answer seems more complete, I leave the source:
How to take partial screenshot with Selenium WebDriver in python?
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
Here they asked a similar question, and the answer seems more complete, I leave the source:
How to take partial screenshot with Selenium WebDriver in python?
from selenium import webdriver
from PIL import Image
from io import BytesIO
fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()
im = Image.open(BytesIO(png)) # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
answered Jun 7 '18 at 20:50
gabriel de la cruzgabriel de la cruz
211
211
add a comment |
add a comment |
I understand you are looking for an answer in python, but here is how one would do it in ruby..
http://watirwebdriver.com/screenshots/
If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.
eg:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.
add a comment |
I understand you are looking for an answer in python, but here is how one would do it in ruby..
http://watirwebdriver.com/screenshots/
If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.
eg:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.
add a comment |
I understand you are looking for an answer in python, but here is how one would do it in ruby..
http://watirwebdriver.com/screenshots/
If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.
eg:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.
I understand you are looking for an answer in python, but here is how one would do it in ruby..
http://watirwebdriver.com/screenshots/
If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.
eg:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.
answered Oct 12 '12 at 6:57
sambeherasambehera
60121129
60121129
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8900073%2fwebdriver-screenshot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown