截取 selenium c# 下拉菜单中的选项 [英] Take screenshot of the options in dropdown in selenium c#

查看:36
本文介绍了截取 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 键的第一个实现在我在网站上完成后手动工作,但没有通过 selenium 工作.第二个实现也不起作用.我也尝试过 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.

我还有一个问题在这里.selenium 真的有可能点击并展开一段时间直到抓取屏幕吗?

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?

任何帮助将不胜感激.

推荐答案

我认为正常的下拉是不可能的.由于带有您可以选择的选项的覆盖显示在本机控件内部和 selenium 可以使用的上下文之外.为此,您需要一些单独的过程或工具来自行捕获桌面或应用程序的屏幕截图.

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天全站免登陆