Selenium Webdriver - Capture URL in screenshot

2019-05-21 18:15发布

问题:

I understand that the screenshot functionality basically works as painting the DOM. Since the URL is not part of the DOM, the screenshot does not contain the URL. But is there any workaround to capture the URL as part of the screenshot?

回答1:

Currently it is not possible with webdriver.

You can follow any of the below approaches. (You did not mention the programming language. These examples are in Java.)

1. Capture current desktop screen

Robot has createScreenCapture method. So, I would go with that & It is very easy to implement.

    Robot robot = new Robot();
    BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(screenShot, "JPG", new File("ScreenShot.jpg"));

2. You can write the URL in the image.

This might be an annoying approach sometimes as it might hide information in the image. But this too is very easy to implement.

Check this answer

3. Add the current URL to the image as meta data

Check this answer



回答2:

Absolutely correct, getting URL in the screen-shot is yet not possible in Selenium and here are the reference Link which tells the same. But as a part of workaround what i can suggest you is to first get the screen shot using

robot.keyPress(KeyEvent.VK_PRINTSCREEN);

Remember this workaround might became time consuming if more number of screen shots has to be taken but can be efficient if you want only one screen shot in single run of your AUT. Second once you copy the screen shot in your clipboard it again depends on you how you want to automate it further, By using Third party tool like Sikuli you can easily integrate it with selenium.

As this is a workaround i would not guarantee success.