2

我一直在寻找一种方法来使用 C# 中的 Selenium 在HTML DOM 之外单击扩展的小部件(比如打开它……),但没有成功。我尝试了 addExtension 方法,但它使 Chrome 崩溃并尝试使用默认配置文件,以便活动扩展将与 ChromeDriver 实例一起加载,但这对我也不起作用。

这些元素没有我可以导航到以更改其设置的页面(单击小部件时它们会弹出一个窗口),因此我需要另一种方法来执行此操作。

你们中的任何 Selenium 专家是否知道我是否可以使用 Selenium 实现它,或者它可能是将来要实现的功能?

或者也许有另一个很好的免费工具可以做到这一点?

4

2 回答 2

0

我在 AutoIt 中找到了这个带有包装器的图像搜索 dll 库,它为我解决了这个问题。这不是您能找到的最强大的解决方案,但对于大多数人来说,它运行良好。它使用 Chrome 的不可识别控件对我有用。

非常简单的使用:

$result = _ImageSearchArea("ButtonImagePath.png", $tolerance, $Left, $Top, $Right, $Bottom, $x, $y, 100)

其中 $x 和 $y 是结果位置,因此您可以在之后发送点击。使用截图工具获取图像也非常容易。更多信息在这里这里

编辑:如果你想在 C# 中使用它,它也可以与它的 dll 进行互操作:(它对于自动单击鼠标等未识别的按钮非常有用)

class ImageHelper
{
    [DllImport("ImageSearchDLL.dll")]
    private static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);

    private static String[] UseImageSearch()
    {
        int right = Screen.PrimaryScreen.WorkingArea.Right;
        int bottom = Screen.PrimaryScreen.WorkingArea.Bottom;

        IntPtr result = ImageSearch(0, 0, right, bottom, "imageFileName.png");
        String res = Marshal.PtrToStringAnsi(result);


        if (res[0] == '0') return null;//not found

        String[] data = res.Split('|');
        int x; 
        int y; 
        int.TryParse(data[1], out x);
        int.TryParse(data[2], out y);

        //0->found, 1->x, 2->y, 3->image width, 4->image height
        Cursor.Position = new Point(x, y);

        return data;
    }
}
于 2014-01-29T15:09:42.790 回答
0

UIAutomation可能是这种情况的最佳解决方案。Chrome 有一个扩展 UI 元素“容器”,您可以在其中找到扩展的小部件作为按钮。这可以通过Visual UI Automation Verify进行验证:

在此处输入图像描述

相关代码将是:

AutomationElement chrome = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));
AutomationElement extensionsContainer = chrome.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Extensions"));

单击小部件后,如果它们仍在 DOM 之外,则很难找到与扩展相关的元素,因此在这种情况下,我认为图像搜索(使用 Sikuli 或其他答案中的类似)是一个很好的尝试。

于 2015-06-24T10:31:26.797 回答