Using SelectByText (partial) with C # Selenium WebDriver bindings doesn't work

I am using Selenium WebDriver Extensions in C # to select a value from a select list using a partial text value (the actual one takes place in front). I can't get it to work using partial text matching. Am I doing something wrong or is this a mistake?

Playable example:

using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support.UI; namespace AutomatedTests { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://code.google.com/p/selenium/downloads/list"); var selectList = new SelectElement(driver.FindElement(By.Id("can"))); selectList.SelectByText("Featured downloads"); Assert.AreEqual(" Featured downloads", selectList.SelectedOption.Text); selectList.SelectByValue("4"); Assert.AreEqual("Deprecated downloads", selectList.SelectedOption.Text); driver.Quit(); } } } 

Provides an error: OpenQA.Selenium.NoSuchElementException: Cannot locate element with text: Featured downloads

+6
source share
2 answers

The SelectByText method is broken, so I wrote my own extension method called SelectBySubText to do what it should do.

 using System.Linq; using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; namespace AutomatedTests.Extensions { public static class WebElementExtensions { public static void SelectBySubText(this SelectElement me, string subText) { foreach (var option in me.Options.Where(option => option.Text.Contains(subText))) { option.Click(); return; } me.SelectByText(subText); } } 
+8
source

If you can reproduce the problem in a simple HTML page, you should definitely submit a bug report.

Looking at the source code, SelectByText does this first:

 FindElements(By.XPath(".//option[normalize-space(.) = " + EscapeQuotes(text) + "]")) 

and if he finds nothing, he does the following:

 string substringWithoutSpace = GetLongestSubstringWithoutSpace(text); FindElements(By.XPath(".//option[contains(., " + EscapeQuotes(substringWithoutSpace) + ")]")) 

Therefore, theoretically, this should work. You can also play with XPath yourself and see if you can make it work in your case.

0
source

Source: https://habr.com/ru/post/913094/


All Articles