0

I want to select a value from a drop down using selenium. the value is "Other" See PIC enter image description here

The xpath for the dropdown is: //nz-select[@formcontrolname='selectedIntegrationTypes']

The page code is: enter image description here

This is my Code:

public static void selectDropDownByXpath()
    {
        WebDriver driver2 = WebDriverMgr.getDriver();
        Select dropDown = new Select(driver2.findElement(By.xpath("//nz-select[@formcontrolname='selectedIntegrationTypes']")));
        dropDown.selectByVisibleText("Other");
    }

And I get this error message:

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "nz-select"
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PC', ip: '12.35.12.65', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: driver.version: unknown

Can someone please advise how can I select the value from the drop down? regards

Bastian
  • 1,089
  • 7
  • 25
  • 74

3 Answers3

1

First of all this is not select tag it is angular nz-select tag, thats what the error also says

Element should have been "select" but was "nz-select"

So you can't use Select class and you need to use normal script standard.

You need to use Xpath like:

//nz-select/ng-reflect-name='selectedIntegrationtypes'

Use standard script like:

driver.findElement(By.xpath("//nz-select/ng-reflect-name='selectedIntegrationtypes'")).click();
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • And After I will do the click, how it can search and select the value that I want? in angular – Bastian Sep 25 '19 at 10:47
  • normal using findElements and add the element in list.. you need to identify the xpath of the values with select all option .. refer: https://stackoverflow.com/questions/36848352/how-to-select-value-from-dropdown-without-using-select-class-becuase-in-have-dr – Shubham Jain Sep 25 '19 at 10:48
0

A Select element expects a 'select' tag. In your case the tag is 'nz-select'. Hence it's throwing an error.

I would write a dropdown class.

   public DropDownMenu(By optionsStrategy, By optionButtonStrategy, IWebDriver driver)
    {
        driver = driver;
        optionContainerStrategy = optionsContainer;
        optionsStrategy = optionsStrategy;
        optionButtonStrategy = optionButtonStrategy;
        optionButton = driver.FindElement(_optionButtonStrategy);           
    }

Where you can pass the locator for your options/item, and the locator for the dropdown button, which will trigger the display of the dropdown option values.

var dropDown = new Dropdown(By.CssSelector("formcontrolname['selectedIntegrationTypes']")),By.CssSelector("[insert options/items identifier here]"), driver)

We can now then create a method for selecting an option

    public void SelectItemByName(string itemName)
    {
        Actions action = new Actions(_driver);
        action.MoveToElement(_optionButton).Click().Build().Perform();
        Thread.Sleep(500);
        GetOption(itemName, _optionsStrategy).Click();
    }

    private IWebElement GetOption(string optionName, By optionStrategy)
    {
        IWebElement optionElement = _driver.FindElements(optionStrategy).Where(x => x.Text.Trim().Trim().Equals(optionName.Trim(), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        return optionElement ?? throw new Exception($"Option {optionName} not found.");
    }

    private IReadOnlyCollection<IWebElement> GetOptions(By optionStrategy) => _driver.FindElements(optionStrategy);   

Then you can use it by dropDown.SelectItemByName("Other")

Liam
  • 27,717
  • 28
  • 128
  • 190
zer0gr4v
  • 62
  • 3
-1

Try selectbyindex function.

Select ddlCCType = new Select(driver.findElement(By.xpath("put xpath here..")));
ddlCCType.selectByIndex("index value..");
Liam
  • 27,717
  • 28
  • 128
  • 190
  • The error message say that `Select` can't be used here. – Guy Sep 25 '19 at 11:22
  • Sorry this is not work, 1. I want to select by value not by index, and still the error is about nz-select and not select – Bastian Sep 25 '19 at 11:24
  • import a namespace import org.openqa.selenium.support.ui.Select; above the class and than try. – Dharit Mehta Sep 25 '19 at 11:25
  • @DharitMehta You should read the error message `Element should have been "select" but was "nz-select"`. You can't use `Select` here. – Guy Sep 25 '19 at 11:25