Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot locate by CssSelector in Selenium WebDriver
    text
    copied!<p>I use the :contains() method but I get the error below:</p> <p>Test Name: TheMahler3Test Test FullName: TestingCssSelector.Mahler3.TheMahler3Test Test Source: c:\Users\amahallati\Desktop\TestContainsSelector\TestingCssSelector\Mahler3.cs : line 50 Test Outcome: Failed Test Duration: 0:00:05.135</p> <p>Result Message: System.InvalidOperationException : An invalid or illegal string was specified Result StackTrace:<br> at OpenQA.Selenium.Support.UI.DefaultWait<code>1.PropagateExceptionIfNotIgnored(Exception e) at OpenQA.Selenium.Support.UI.DefaultWait</code>1.Until[TResult](Func`2 condition) at TestingCssSelector.Mahler3.TheMahler3Test() in c:\Users\amahallati\Desktop\TestContainsSelector\TestingCssSelector\Mahler3.cs:line 59</p> <p>This is the page's source code:</p> <pre><code>@{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="width=device-width" /&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;div id="myDiv"&gt; &lt;select name="mySelectInput"&gt; &lt;option value=""&gt;Select one...&lt;/option&gt; &lt;option value="1"&gt;AT&amp;T&lt;/option&gt; &lt;option value="2"&gt;TMobile&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And this is the WebDriver C# code:</p> <pre><code>using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.IE; using OpenQA.Selenium.Support.UI; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Interactions; namespace TestingCssSelector { [TestFixture] public class Mahler3 { private IWebDriver driver; private StringBuilder verificationErrors; private string baseURL; private bool acceptNextAlert = true; [SetUp] public void SetupTest() { driver = new FirefoxDriver(); baseURL = "http://localhost:49638/"; verificationErrors = new StringBuilder(); } [TearDown] public void TeardownTest() { try { driver.Quit(); } catch (Exception) { // Ignore errors if unable to close the browser } Assert.AreEqual("", verificationErrors.ToString()); } [Test] public void TheMahler3Test() { driver.Navigate().GoToUrl(baseURL + "/"); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(40)); wait.Until(d =&gt; { return driver.FindElement(By.XPath("/html/body/div/div/select")); }); driver.FindElement(By.XPath("/html/body/div/div/select")).Click(); wait.Until(d =&gt; { return driver.FindElement(By.CssSelector("option:contains('AT&amp;T')")); }); driver.FindElement(By.CssSelector("option:contains('AT&amp;T')")).Click(); // ERROR: Caught exception [ReferenceError: selectLocator is not defined] } private bool IsElementPresent(By by) { try { driver.FindElement(by); return true; } catch (NoSuchElementException) { return false; } } private bool IsAlertPresent() { try { driver.SwitchTo().Alert(); return true; } catch (NoAlertPresentException) { return false; } } private string CloseAlertAndGetItsText() { try { IAlert alert = driver.SwitchTo().Alert(); string alertText = alert.Text; if (acceptNextAlert) { alert.Accept(); } else { alert.Dismiss(); } return alertText; } finally { acceptNextAlert = true; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload