Dropdown Selenium C#

Asked

Viewed 1,171 times

0

I am trying to select a dropdown with Selenium C#, but as we can see in the HTML below there is no "ID" or "Name", below the code of how I am doing, could help me?

<select ng-model="vm.activeWhatsapp.channel_id" 
                    ng-options="channel.id as channel.display_name group by channel.sector.name for channel in vm.whatsappChannels" 
                    class="ng-pristine ng-valid ng-empty ng-touched">
                    <option value="?" selected="selected">
                    </option>
                    <optgroup label="Itaú Cartões Ativo">
                    <option label="Liderança - Itaú Cartões Ativo" value="number:1325">Liderança - Itaú Cartões Ativo</option>
                    </optgroup>
                    </select>




    //Selecting a drop down control
    public static  void SelectDropDown(IWebDriver driver, string element, string value, string elementtype)
    {

        if (elementtype == "Id")
            new SelectElement(driver.FindElement(By.Id(element))).SelectByText(value);
        if (elementtype == "Name")
            new SelectElement(driver.FindElement(By.Name(element))).SelectByText(value);
        if (elementtype == "Class")
            new SelectElement(driver.FindElement(By.CssSelector(element))).SelectByValue(value);

SeleniumSetMethods.SelectDropDown(drive, ".ng-pristine.ng-valid.ng-empty.ng-touched", "1325", "Class");

3 answers

0

Look I know how it is, not having an ID or Name hurts us a lot, usually I use Selenium’s Xpath to get the value it goes straight into the tag, see if the example below helps you:

SelectElement Selec_Event = new SelectElement(itauPednO9.FindElement(By.Xpath("//*[@id="navbar"]/ul/li[3]/a")));
                Selec_Event.SelectByText(value);

See what you can do

Atte!

0

You can access via classpath. Use the F12 and follow this structure here:

//*[@<elemento desejado, pode ser id, name etc>='nome']<caminho ex:/tbody/tr> //*[contains(text(),'<texto que se encontra na tag>')] 

In case you have any doubts, talk to me.

-1

It is possible to find an element in various ways in Selenium. Particularly when there is no ID, or NAME, or when the ID or NAME are duplicated, I prefer to use the CSS Selector because I find it more "understandable" than Xpath.

But for both CSS and Xpath there is an interesting feature in Chrome. By squeezing F12, Enter the element you want to obtain the data, press the Right Mouse Button, go to Copy and select the desired one ( CSS Selector or Xpath )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.