2

In order to get a browser window title, the web driver must switch to the window by its Handle id:

string title = driver.SwitchTo().Window("Handle ID").Title;

I am testing opening multiples windows and I need to get a window's title by only having the Handle Id and also without the web driver focusing the selected window bringing it to the front. This is what I have:

public string getWindowTitleByHandleId(string handleId){
    string[] handles = driver.WindowHandles.ToArray();
    for (int i = 0; i < handles.Length; i++)
    {
        if (handles[i] == handleId) { return driver.SwitchTo().Window(handles[i]).Title}
    }
    return "";
}

Is there a better way to achieve this?

5
  • This question is really confusing. Are you looking to get all of the window titles, just the current one...? More clarification is needed here. – Brian Jan 17 '19 at 16:55
  • Done, sorry about that. I am trying to get only one window's title by searching by its Handle Id. Let's say I have 100 windows opened and I need to retrieve the title of window N° 37. – Lolpez Jan 17 '19 at 19:02
  • And are you getting any title at all, at this time? – Brian Jan 17 '19 at 19:15
  • Yes, but the driver focuses the window I am searching for. And I have to switch it back to where it left. Looks ugly and probably there is a better way to do it. Not to mention that maybe Selenium web driver have a method for this case. – Lolpez Jan 17 '19 at 19:21
  • Did you find a better way of doing this? I am using winappdriver and switching a window to find the title has the side effect of closing some of my windows that close when they loose focus, so having a way of enumerating them all without switching window would be very handy. – Jonny Feb 20 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.