0

I'm trying to get all the url's of google chrome in windows forms with c#. Currently I had tried with an AutomationElement which gives me only active tab url instead of all opened tabs.

My code is like below:

public void GetAllUrls()
{
    Process[] procsChrome = Process.GetProcessesByName("chrome");
    foreach (Process chrome in procsChrome)
    {
        // the chrome process must have a window
        if (chrome.MainWindowHandle == IntPtr.Zero)
        {
            continue;
        }

        // find the automation element
        AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
        AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
          new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));

        // if it can be found, get the value from the URL bar
        if (elmUrlBar != null)
        {
            AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
            if (patterns.Length > 0)
            {
                ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                Console.WriteLine("Chrome URL found: " +  val.Current.Value);
                listBox1.Items.Add(val.Current.Value);
            }
        }
    }
}
IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
Srikanth Reddy
  • 447
  • 1
  • 8
  • 23
  • 2
    All modern web browsers use the [WebDriver API](https://developer.mozilla.org/en-US/docs/Web/WebDriver) for automation. You could use eg Selenium to control the browser and get all open windows (that's what tabs are, as far as the browser is concerned) with `WebDriver.GetWindowHandles`. Check [Selenium C# Tutorial: Handling Multiple Browser Windows](https://www.lambdatest.com/blog/selenium-c-tutorial-handling-multiple-browser-windows/) – Panagiotis Kanavos Oct 06 '20 at 07:26
  • @PanagiotisKanavos , I tried with selenium too. I had Inherited the IWebDriver to my form class. There after I found multiple methods in that interface. Do you have any suggestion in which method I needs to add my logic ? – Srikanth Reddy Oct 06 '20 at 08:09
  • 1
    None. You *use* the IWebDriver interface provided by Selenium, you don't inherit from it. I already posted a link to a tutorial that shows how to *use* Selenium to drive a browser and even work with multiple tabs – Panagiotis Kanavos Oct 06 '20 at 08:11
  • It's asking to install "chromedriver.exe". We can't sure that end users will install this driver. is it not possible without chromedriver ? – Srikanth Reddy Oct 06 '20 at 08:35
  • It's not an OS driver, it's just a program, one [provided by the browser vendor](https://chromedriver.chromium.org/downloads). That's like asking if your clients would install *your* program. Or if they'd install Chrome in the first place. The driver doesn't need an installer, you can just copy it with your application. It does have to be compatible with the installed browser version though – Panagiotis Kanavos Oct 06 '20 at 08:40
  • What are you trying to do? Despite the name, UI Automation is an assistive technology. WebDriver is used specifically to automate browsers. You don't need either of them if you want to make HTTP calls and parse HTML. People do use Selenium and WebDriver as a quick & dirty (but slow) screen scraper though – Panagiotis Kanavos Oct 06 '20 at 08:43
  • My exact need is : I needs to track the end user opened tab URL's as a list. For example if I open the "gmail", "facebook",... etc. All the opened URL's needs to make as list. Currently with "AutomationElement" I was able to fetch active tab URL only instead of all. So I'm trying with selenium as you suggested. – Srikanth Reddy Oct 06 '20 at 09:01
  • That's not what you try to do, that's how you think it can be done. The what is building an assistive application, trying to automate a web app through the browser instead of using HTTP calls, or trying to build a screen scraper, again without using HTTP calls. Perhaps you're trying to automate an SPA application without calling the APIs? – Panagiotis Kanavos Oct 06 '20 at 09:08

0 Answers0