Miss QA
Miss QA

Reputation: 55

C# and selenium: selectByText using a dropdownlist

I have a list of elements that am unable to call from a selectByText. If I use "Test1" instead of dropdownLists[i], the code works, however I want to loop through a list of 10 items

Code:

static void dropdownLists()
{
    dropdownList = new List<string>();
    dropdownList.Add("Test1");
    dropdownList.Add("Test2");
    dropdownList.Add("Test3");                
}

//Used in a for loop

for (int i = 0; i < dropdownList.Count; i++)
{
       System.Console.WriteLine("dropdownList: {0}", dropdownList[i]);
       new SelectElement(driver.FindElement(By.Id("DocumentType")))
       .SelectByText(dropdownLists[i]);
       Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
}

The error I receive is:

Error 2 Argument 1: cannot convert from 'method group' to 'string' C:\myCSharp\mySelenium\mySelenium\ProgramCRM1.cs 76 91 myInfoCenter

Error 3 Cannot apply indexing with [] to an expression of type 'method group' C:\myCSharp\mySelenium\mySelenium\ProgramCRM1.cs 76 91 myInfoCenter

Error 1 The best overloaded method match for 'OpenQA.Selenium.Support.UI.SelectElement.SelectByText(string)' has some invalid arguments C:\myCSharp\mySelenium\mySelenium\ProgramCRM1.cs 76 17 myInfoCenter

Upvotes: 3

Views: 1087

Answers (2)

Saifur
Saifur

Reputation: 16201

Easier things to do in this case will be SelectByIndex(i) You are over complicating the implementation I guess

If you want to stick to your plan I guess the following is a better option as well:

var selectElement = new SelectElement(Driver.FindElement(By.Id("DocumentType")));
int options = selectElement.Options.Count;

for (int i = 0; i < options; i++)
{
    Console.WriteLine(selectElement.Options[i].Text);
}

EDIT

To meet OP's criteria

By byXpath = By.XPath("//select[@id='DocumentType']");

//wait for the element to exist
new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(byXpath));

IWebElement element = Driver.FindElement(byXpath);
var selectElement = new SelectElement(element);
int options = selectElement.Options.Count;

for (int i = 0; i < options; i++)
{
    Console.WriteLine(selectElement.Options[i].Text);

    //in case if other options refreshes the DOM and throws StaleElement reference exception
    new SelectElement(Driver.FindElement(byXpath)).SelectByIndex(i);

    //do the rest of the stuff
}

Upvotes: 1

jiulongw
jiulongw

Reputation: 355

The error message pretty much told you everything you need.

  1. 'OpenQA.Selenium.Support.UI.SelectElement.SelectByText(string)' has some invalid arguments. So you're not passing a string to the method.

  2. Error 3 Cannot apply indexing with [] to an expression of type 'method group'. What? [] cannot be applied to dropdownLists? Why?

  3. Error 2 Argument 1: cannot convert from 'method group' to 'string'. Oh, that is because dropdownLists is a function name, not a variable!

Now you should know your mistake: you used a function name dropdownLists instead of variable name dropdownList.

BTW, try to name your function better. Usually adding a verb in the beginning will make things much clearer. For example, getDropdownList or populateDropdownList.

Upvotes: 2

Related Questions