Hi,
I currently have an issue with “capture_page_screenshot” method in Robot Framework. This keyword captures the entire web page.
Objective:
My overall objective is to have a custom keyword “Capture Screenshot Of Element” which I can pass an element too and just return a screenshot of that element, expanding on the Capture Page Screenshot keyword.
It would be called using Robot Framework as follows: Capture Screenshot Of Element elementName imagename.jpg
Current Work Done:
I have coded this new keyword similar to below:
def Capture_Screenshot_Of_Element(self, element, filename):
location = element.location
size = element.size
self.capture_page_screenshot(filename) # saves screenshot of element
im = Image.open(filename) # 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(filename) # saves new cropped image
The above function takes the screenshot but the screenshot is not a true reflection of what is seen in the browser (see screenshot1.jpg)
When I write a small Python script using Selenium as below I get the correct reflection of what is seen on the browser (screenshot.jpg)
fox = webdriver.Firefox()
fox.get('URL GOES HERE')
element = fox.find_element_by_xpath(‘XPATH_LOCATION’) # find part of the page you want image of
location = element.location
size = element.size
fox.save_screenshot('screenshot.jpg') # saves screenshot of element
fox.quit()
im = Image.open('screenshot.jpg') # 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.jpg') # saves new cropped image
Questions:
What is the difference between Robot Framework’s (Selenium2Library) capture_page_screenshot function and Selenium’s save_screenshot function?
Is there a solution to obtaining the correct image from a browser using Robot Framework?
Ugh
The difference is very small. There is some logic on the keyword:
1) Determine where the screen shot is saved.
2) Determine the actual file name.
3) And make the screen shot visible in the log file.
But the actually getting the screen shot, it relies on the selenium functionality [1], which you are using. But I can see that there is a difference in the screen shots, but without being able try it out it is, for me, impossible to say anything concrete. As a wild guess, I would assume that this is a timing issue. As a first thing, I would try small sleep and if that solves the problem, I would then perform better polling to make sure that required elements are in place and visible.
-Tatu
Send from my mobile
[1] https://github.com/robotframework/Selenium2Library/blob/master/src/Selenium2Library/keywords/_screenshot.py