Search code examples
c#jqueryweb-scrapingwatinbots

WatiN C# selecting item from a HTML generated list


I'm trying to select an item from a html generated list in this webpage :

https://accounts.google.com/SignUp

For example the Birthday listbox has been tested with the following with no success:

browserInstance.SelectList(Find.ByClass("goog-inline-block goog-flat-menu-button jfk-select")).Focus();
browserInstance.SelectList(Find.ByClass("goog-inline-block goog-flat-menu-button jfk-select")).Click();

This doesn't work also:

Div birth = browserInstance.Div(Find.ByClass("goog-inline-block goog-flat-menu-button jfk-select"));
birth.Click();

The click does not trigger , no list appear as it does when you click the div in the UI.

How can I select an item from the list?


Solution

  • If you see the HTML of the page, it is not referred as Select List instead it is a list of Div's where each element is referred to a single month. You can use the below code to select a random month from the drop down list.

    browserInstance.FindElement(By.Id("BirthMonth")).Click();
    System.Threading.Thread.Sleep(1000);
    System.Collections.ObjectModel.ReadOnlyCollection<IWebElement> months = browserInstance.FindElement(By.ClassName("goog-menu-vertical")).FindElements(By.ClassName("goog-menuitem-content"));
    months[new Random().Next(0, 11)].Click();
    System.Threading.Thread.Sleep(1000);
    

    PS: Used delay for testing, you can update it based on your need.