【问题标题】:Take screenshot of the options in dropdown in selenium c#截取 selenium c# 中下拉列表中的选项的屏幕截图
【发布时间】:2023-03-22 06:33:01
【问题描述】:

我想使用 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();

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

我还有一个问题。 selenium 真的可以点击并展开一会,直到抢屏吗?

任何帮助将不胜感激。

【问题讨论】:

  • 有什么原因你不想只提取选项的文本并将其写入日志而不是屏幕截图?
  • @JeffC 我可以提取下拉菜单的选项并将其写入日志,但我的客户需要选项列表的屏幕截图
  • @KarthikChintala 你已经截图了吗?有什么问题?
  • @Manu 我没有。我想要。问题在于使用下拉选项截取屏幕截图。我不需要选项列表,只需要这些选项的屏幕截图,如图所示。
  • @KarthikChintala 您能否添加屏幕的外观和选项的 DOM 结构?另外,我知道你只需要网页元素截图,对吧?

标签: c# selenium c#-4.0 drop-down-menu screenshot


【解决方案1】:

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

Link

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

对于 C#,您可以使用 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());
}

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

Selenium 只能截取使用 Javascript/CSS 制作的自定义下拉菜单中的选项,而不能截取选择下拉菜单中的选项。

如果上述代码有效或您需要更多帮助,请告诉我。

【讨论】:

  • 感谢您的回答马努。我知道为整个页面和特定元素截取屏幕截图。我已经阅读了您在答案中提到的报价。我想是否有任何其他方式可以单击下拉菜单,使其展开,然后截取页面。
  • 问题是您无法使用 selenium 扩展选择下拉菜单来截取屏幕截图。这就是限制。
  • 感谢您的努力。我确实设法点击了下拉列表中的选项,但是 selenium 无法获取扩展选项的页面截图。是的,看起来 selenium 无法抓取这些屏幕截图。
【解决方案2】:

相信我,无需任何其他工具即可轻松完成。

您只需在点击选择元素后执行以下步骤:

  1. 制作页面截图
  2. 获取您的 Select 元素位置(其在页面上的坐标)
  3. 获取最后一个 Option 元素位置
  4. 获取最后一个 Option 元素大小
  5. 创建与选择和位置相同的新矩形

    var screenshotSize = lastOption.Position - Select.Position;

    screenShotSize.height += lastOption.Size.Height;

  6. 从屏幕截图中截掉除此 Rectangle 之外的所有内容

如果是 c#,您可以使用 #6 以下代码

Bitmap ScreenshotOfSelect = browserScreenshot.Clone(new Rectangle(point, size), screen.PixelFormat);

因此,您将收到仅包含扩展 Select =) 的位图

如您所见,它真的非常容易做到 =) 除了 selenium,您不需要任何工具。并且 浏览器窗口可能暂时不可见(例如使用 phantomJS)

【讨论】:

    【解决方案3】:

    要打开下拉列表,您只需 .Click() 元素。所以在你的情况下,

    IWebElement element = Driver.FindElement(By.Id("carsId"));
    element.Click();
    

    将展开下拉菜单。问题是 Selenium 的屏幕截图功能没有捕获打开的下拉菜单。您可以通过使用 .NET 来解决这个问题,只需截取活动窗口的屏幕截图。下面的工作示例代码。

    static void Main(string[] args)
    {
        IWebDriver Driver = new FirefoxDriver();
        Driver.Navigate().GoToUrl("http://www.tutorialspoint.com/html/html_select_tag.htm");
        Driver.Manage().Window.Maximize();
        IWebElement element = Driver.FindElement(By.Name("dropdown"));
        element.Click();
        TakeScreenShotOfWindow(@"C:\sshot.png");
    }
    
    // from http://stackoverflow.com/a/363008/2386774
    public static void TakeScreenShotOfWindow(string filePath)
    {
        // Create a new bitmap.
        var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
    
        // Create a graphics object from the bitmap.
        var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    
        // Take the screenshot from the upper left corner to the right bottom corner.
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
    
        // Save the screenshot to the specified path that the user has chosen.
        bmpScreenshot.Save(filePath, ImageFormat.Png);
    }
    

    【讨论】:

    • 让我们挑剔吧 :-) 这是屏幕截图,而不是浏览器窗口,另外浏览器窗口可能在另一个屏幕上(辅助监视器,...)。
    • OK...如果浏览器在屏幕上,那么它是浏览器的屏幕截图。 :) 是的,浏览器可能会在不同的屏幕上运行。我的猜测是它在实验室机器上运行,可能只有一个显示器。您还可以控制浏览器出现在哪个屏幕上。直截了当的方法行不通,但这确实可以作为问题的答案,并且实现起来非常简单。
    • 旧帖子,但这实际上是我见过的 C# 的最佳解决方案。使用常用的 Screenshot.Clone 方法通常会导致内存不足异常,而这种方法不会。您还可以使用它来截取特定元素的屏幕截图,而不是使用元素的 X 和 Y 值等来截取整个屏幕
    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多