【问题标题】:How to manipulate Chrome extension's widget with Selenium or another tool?如何使用 Selenium 或其他工具操作 Chrome 扩展的小部件?
【发布时间】:2013-08-16 15:58:47
【问题描述】:

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

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

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

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

【问题讨论】:

  • 我相信这样的事情是不可能的。不过我很高兴被证明是错误的。
  • 我知道这可能不是您想要的解决方案,因为这不会将句柄返回到按钮,但现在它是一个很好的解决方法......

标签: c# google-chrome google-chrome-extension selenium-webdriver


【解决方案1】:

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

非常简单的使用:

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

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

编辑:如果你想在 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;
    }
}

【讨论】:

    【解决方案2】:

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

    相关代码为:

    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 或其他答案中的类似)是一个不错的选择试试看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2018-06-13
      • 2013-06-16
      • 2018-07-16
      • 2021-09-20
      相关资源
      最近更新 更多