1

I have a dropdown which structure is this one: enter image description here

Now, I want to use Selenium Webdriver to select the last option by text (I cannot rely on the option being the last one actually).

Trying this:

var text = "   undersida";

var option = new SelectElement(browser.FindElement(By
  .CssSelector("#Menu_ParentMenuID"))).SelectByText(text);

gives me a NoSuchElementException.

Why?

Here is the list of values I have tried for text:

"   undersida"
"   undersida"
"undersida"
Anders Lindén
  • 6,839
  • 11
  • 56
  • 109

4 Answers4

1

I don't know if this is still an issue, but i'd like to contribute.

I think the problem lies in the nature (or your understanding) of a "no-break space".
it is not a regular space like in " " or the string " "
what you actually want is the c# unicode representation of it

so the string to test should be something like:

"\u00A0\u00A0\u00A0undersida"

take a look here: http://www.fileformat.info/info/unicode/char/a0/index.htm

bobbor
  • 141
  • 5
0

What about this?

new Select(browser.findElement(By.css("#Menu_ParentMenuID")))
    .selectByVisibleText("   undersida");
Turcia
  • 653
  • 1
  • 12
  • 29
0

There is one other way to select the concerned element, if you are not bound by selecting by visible text only.

Below is the Java code for that:

        String str = "undersida";

        Select sel = new Select(browser.findElement(By.id("Menu_ParentMenuID")));
        List<WebElement> list_options = sel.getOptions();
        for(WebElement ele: list_options){
            if(ele.getText().contains(str)){
                String value = ele.getAttribute("value");
                sel.selectByValue(value);
                break;
            }
        }

This will get all the options from the select dropdown. Then search the option whose innerHTML/text contains the required one. Then, it gets the "value" attribute of that option. After that, it selects the concerned item from the dropdown using the value related to that text.

Subh
  • 4,354
  • 1
  • 13
  • 32
  • Where do I find the Select class? – Anders Lindén Nov 27 '14 at 15:38
  • From your code, I perceive you are using **C#** for coding. So, you can use **"SelectElement"** class, and this link might help for properly selecting using **SelectElement** : [http://stackoverflow.com/a/15535624/4193730](http://stackoverflow.com/a/15535624/4193730). Also, if you can formulate the above java code in C#, it will definitelywork. – Subh Nov 27 '14 at 15:57
0

Try using the following code:-

var option = new SelectElement(browser.FindElement(By.CssSelector("#Menu_ParentMenuID"))).SelectBySubText("undersida");
Tripti Mittal
  • 71
  • 2
  • 7