在selenium c#的下拉列表中选择选项的截图 [英] Take screenshot of the options in dropdown in selenium c#

查看:445
本文介绍了在selenium c#的下拉列表中选择选项的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用selenium c#捕获下拉列表中显示的选项的截图,就像下面显示的图像一样。

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 tried multiple ways to take the screenshot. Basically I've to expand the dropdown of the element to capture the screenshot. Here is what I've done

//#1
var element = Driver.FindElement(By.Id("carsId"));
Actions builder = new Actions(Driver);
builder.SendKeys(element, Keys.LeftAlt + Keys.Down).Build().Perform();

//#2
Actions act = new Actions(Driver);
act.MoveToElement(element).Build().Perform();

第一个实现按 Alt + Down 当我在网站上完成但是没有通过硒工作时,手动进行操作。第二个实施也没有。我也尝试过 builder.ClickAndHold()方法。

The first implementation to press Alt + Down keys worked manually when I've done on the site but didn't work through selenium. The second implementation didn't work either. I've also tried builder.ClickAndHold() method as well.

我还有另一个问题。硒真的有可能点击并展开一段时间直到抓住屏幕?

And I've another question over here. Is it really possible for selenium to click and expand for a while until to grab the screen?

任何帮助将不胜感激。

推荐答案


我不认为这是正常的下拉菜单。由于可以选择的选项的叠加显示在本机控件之外,并且不在硒可以使用的上下文之外。为此,您需要一些独立的过程或工具,可以捕获桌面或应用程序自己的屏幕截图。

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.

链接

现在,要捕获桌面/应用程序的屏幕截图,我们使用 Robot 对象。

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

对于C#,您可以使用捕获活动窗口的屏幕截图

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

机器人样本代码

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只能在使用Javascript / CSS制作的自定义下拉菜单中选择选项的截图,而不能在选择下拉列表中。

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.

这篇关于在selenium c#的下拉列表中选择选项的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆