C # and selenium: selectByText using dropdown

I have a list of items that cannot be called from selectByText. If I use "Test1" instead of dropdownLists [i] the code works, however I want to scroll through the 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();
}

      


Received error:

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

Error 3 Could not apply indexing with [] to expression like "method group" C: \ myCSharp \ mySelenium \ mySelenium \ ProgramCRM1.cs 76 91 myInfoCenter

Error 1 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

+3


source to share


2 answers


The easiest way to do in this case would be SelectByIndex(i)

you over complicate the implementation, I think

If you want to stick with your plan, I think the following is better:

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 the OP 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
}

      

+1


source


The error message pretty much told you everything you need.

  • 'OpenQA.Selenium.Support.UI.SelectElement.SelectByText(string)' has some invalid arguments

    ... This way you are not passing a string to the method.

  • Error 3 Cannot apply indexing with [] to an expression of type 'method group'

    ... What kind? [] cannot be applied to dropdownLists

    ? Why?

  • Error 2 Argument 1: cannot convert from 'method group' to 'string'

    ... Oh, that's because dropdownLists

    - it's a function name, not a variable!



Now you should know your mistake: you used the function name dropdownLists

instead of the variable name dropdownList

.

By the way, try to name your function better. Usually adding a verb at the beginning will make things a lot clearer. For example, getDropdownList

or populateDropdownList

.

+2


source







All Articles