【问题标题】:How to get active Google profile name when I get the active Url from google chrome browser using C#当我使用 C# 从 google chrome 浏览器获取活动 URL 时如何获取活动的 Google 个人资料名称
【发布时间】:2018-04-24 18:11:53
【问题描述】:

我正在使用此代码从 Google Chrome 浏览器获取当前活动的标签页打开 URL。

 public static string GetActiveTabUrl()
    {
        Process[] procsChrome = Process.GetProcessesByName("chrome");

        if (procsChrome.Length <= 0)
            return null;

        foreach (Process proc in procsChrome)
        {
            // the chrome process must have a window 
            if (proc.MainWindowHandle == IntPtr.Zero)
                continue;

            // to find the tabs we first need to locate something reliable - the 'New Tab' button 
            AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
            var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
            if (SearchBar != null)
                return (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
        }

        return null;
    }

有什么方法可以让我也返回当前的个人资料名称?我的意思是在 google chrome 浏览器的右上方可以找到配置文件名称,因此它可能不仅仅是具有不同名称的配置文件,我只想使用来自此函数的 URL 获取当前名称。我可以在 google chrome 中同时打开或使用多个配置文件,因此我想获取活动标签 URL 和配置文件名称以及该配置文件的电子邮件。
这是配置文件名称的图像,我希望它从 chrome 与当前活动的标签 URL 一起发送。
所以我希望类似的事情可以在 c# 中完成以获取配置文件名称
var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "profile email"));

最后一件事它可以是 c# 或 java 代码,如果它可以更好地在其中做也可以用任何编程语言获得它的任何解决方案,请帮助提供任何信息。
提前致谢。

【问题讨论】:

    标签: java c# google-chrome selenium selenium-chromedriver


    【解决方案1】:

    您将不得不深入了解 HTML。使用 chrome 的开发人员功能并检查 HTML。您将找到用户名所在的类,文本存储为标题,请在下面找到示例。

    <a class="gb_b gb_ib gb_R" href="https://accounts.google.com/" role="button" tabindex="0" title="Google Account: FillerFirstName FillerLastName (fillerEmail@gmail.com)" aria-expanded="false"><span class="gb_db gbii"></span></a>
    

    您可以使用 C# 以多种不同的方式实现此目的。我会推荐 Selenium。

    【讨论】:

    • 我尝试对其进行一些搜索,但没有,您能否在答案中添加任何代码,即使使用 Selenium 库也可以获取配置文件名称?
    • 这里有一个链接可以帮助你理解它。 codeproject.com/Articles/1053815/…
    • 您将需要使用 findElement 关键字。这将允许您在我在上面的答案中显示的 html 中找到用户名。找到对象后,您可以显示包含用户名的标题标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2015-11-29
    相关资源
    最近更新 更多