Take screenshot of the options in dropdown in selenium c#

前端 未结 3 1675
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 11:34

I\'d like to capture the screenshot of the options that are displayed in the dropdown using selenium c# just like the image that is displayed below.

I\'ve t

3条回答
  •  隐瞒了意图╮
    2021-01-17 12:07

    I don't think it'll be possible for normal drop downs. Since the overlay with the options you can choose from are displayed inside a native control and outside of the context of what selenium can work with. For this, you'll need some separate process or tool that can capture the screenshot of the desktop or application it self.

    Link

    Now, to capture the screenshot of desktop/application, we use Robot objects in Java.

    For C#, you can use methods suggested in Capture screenshot of active window?.

    Robot Sample Code:

    try {
    
        //Get the size of the screen stored in toolkit object
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
    
        //Create Rectangle object using height and width of screen
        //Here entire screen is captured, change it if you need particular area
        Rectangle rect = new Rectangle(0, 0, d.width, d.height);  
    
        //Creates an image containing pixels read from the screen 
        Robot r = new Robot();
        BufferedImage img = r.createScreenCapture(rect);
    
        //Write the above generated buffered image to a file
        File f = new File("myimage.jpg");
    
        //Convert BufferedImage to a png/jpg image
        ImageIO.write(img, "jpg", f);
    
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    

    This will take the screenshot of the entire screen and save it into the file on given file location.

    Selenium can only take screenshot of options in custom dropdowns made using Javascript/CSS and not in select dropdown.

    Let me know if above code works or you need more help.

提交回复
热议问题