Selenium webdriver (tested ChromeDriver) cant find option by text containing umlauts?

1.1k views Asked by At

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"
4

There are 4 answers

1
Turcia On

What about this?

new Select(browser.findElement(By.css("#Menu_ParentMenuID")))
    .selectByVisibleText("   undersida");
2
Subh On

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.

0
Tripti Mittal On

Try using the following code:-

var option = new SelectElement(browser.FindElement(By.CssSelector("#Menu_ParentMenuID"))).SelectBySubText("undersida");
0
bobbor On

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 "&nbsp;"
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