在selenium c#中的下拉列表中截取选项的屏幕截图

Kar*_*ala 13 c# selenium screenshot c#-4.0 drop-down-menu

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

在此输入图像描述

我尝试了多种方法来截取屏幕截图.基本上我要扩展元素的下拉列表以捕获屏幕截图.这就是我所做的

//#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();
Run Code Online (Sandbox Code Playgroud)

当我在网站上完成但没有通过selenium工作时,按下Alt+ Down键的第一个实现工作.第二个实现也不起作用.我也试过builder.ClickAndHold()方法.

我在这里还有另一个问题.是否真的有可能硒点击并扩展一段时间,直到抓住屏幕?

任何帮助将不胜感激.

Man*_*anu 3

我认为正常的下降是不可能的。由于包含您可以选择的选项的覆盖层显示在本机控件内,并且在 selenium 可以使用的上下文之外。为此,您需要一些单独的进程或工具来捕获桌面或应用程序本身的屏幕截图。

关联

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

对于 C#,您可以使用捕获活动窗口的屏幕截图中建议的方法?

机器人示例代码:

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());
}
Run Code Online (Sandbox Code Playgroud)

这将截取整个屏幕的屏幕截图并将其保存到给定文件位置的文件中。

Selenium 只能对使用 Javascript/CSS 制作的自定义下拉菜单中的选项进行屏幕截图,而不能对选择下拉菜单中的选项进行屏幕截图。

让我知道上面的代码是否有效或者您需要更多帮助。