There is a site, it has 2 combos, you need to focus on the 1st combobox and select an element in it, then put the focus on the 2nd combobox and select an element in it. Here is the code

HtmlWindow frames = webBrowser1.Document.Window.Frames[0]; frames = webBrowser1.Document.Window.Frames[0]; var elements1 = frames.Document.GetElementsByTagName("select"); elements1[0].Focus(); SendKeys.Send("{DOWN}"); elements1[1].Focus(); 

As you can see, the elements inside the combo box I choose by imitating a key down on the keyboard. The result is that the selection takes place in the second box, it feels like it doesn’t have time to put the focus, as the key is pressed down. Can you please tell me how to fix this? Or maybe there is a more sensible way to select elements of a combo box, rather than simulating keystrokes. Thank you in advance!

    1 answer 1

    I use this construction, it works stably, you can certainly add checks if there is a need:

     #region Выбор элемента в выпадающем списке /// <summary> /// Выбор элемента в выпадающем списке /// </summary> /// <param name="aValue">Текст пункта который необходимо выбрать в выпадающем списке</param> /// <returns>В случае невозможности выбора False</returns> public bool SelectItemFromListbox(string aValue) { HtmlElementCollection a_tag = FCurrentWebBrowser.Document.Window.Frames[0].Document.GetElementsByTagName("select"); foreach (HtmlElement he in a_tag) { if (he.GetAttribute("name").Equals("ИМЯ СПИСКА")) { he.SetAttribute("value", aValue); return true; } } return false; } #endregion #region Ожидание полной загрузки веб страницы Webbrowser /// <summary> /// Ожидание полной загрузки веб страницы Webbrowse /// </summary> /// <param name="aCurWebBrowser">Ссылка на WebBrowser завершение загрузки веб страницы в котором следует дождаться</param> public static void FullLoadWebPage(WebBrowser aCurWebBrowser) { while (aCurWebBrowser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } } #endregion 
    • Many thanks, works! - Pyrejkee
    • one
      you're welcome! - NMD
    • Maybe you can tell me why if I press 2 values ​​of the combo box and one more button with one button, then everything works as it should, but if I put this code in the loop, the program hangs. Although the exit from the cycle is logical. - Pyrejkee
    • put BreakPoint in a loop and see if it happens or not, debugging is useful - NMD
    • I have already run several times through debugging ... it feels like because I don’t press the button myself, but in a cycle, the site doesn’t have time to respond to requests, and therefore it may not be able to exit the cycle at all - Pyrejkee