无法通过Selenium WebDriver中的CssSelector找到

时间:2013-12-30 19:26:31

标签: selenium css-selectors

我使用:contains()方法,但我得到以下错误:

测试名称:TheMahler3Test 测试FullName:TestingCssSelector.Mahler3.TheMahler3Test 测试源:c:\ Users \ amahallati \ Desktop \ TestContainsSelector \ TestingCssSelector \ Mahler3.cs:第50行 测试结果:失败 测试持续时间:0:00:05.135

结果消息:System.InvalidOperationException:指定了无效或非法字符串 结果StackTrace:
在OpenQA.Selenium.Support.UI.DefaultWait 1.PropagateExceptionIfNotIgnored(Exception e) at OpenQA.Selenium.Support.UI.DefaultWait 1.Until [TResult](Func`2条件) 在C:\ Users \ amahallati \ Desktop \ TestContainsSelector \ TestingCssSelector \ Mahler3.cs中的TestingCssSelector.Mahler3.TheMahler3Test()中:第59行

这是该页面的源代码:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <div id="myDiv">
            <select name="mySelectInput">
                <option value="">Select one...</option>
                <option value="1">AT&T</option>
                <option value="2">TMobile</option>
            </select>
        </div>
    </div>
</body>
</html>

这是WebDriver C#代码:

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 =>
            {
                return driver.FindElement(By.XPath("/html/body/div/div/select"));
            });
            driver.FindElement(By.XPath("/html/body/div/div/select")).Click();

            wait.Until(d =>
            {
                return driver.FindElement(By.CssSelector("option:contains('AT&T')"));
            });
            driver.FindElement(By.CssSelector("option:contains('AT&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;
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

contains不是CSS选择器规范的一部分,因此不起作用。

我们都知道并喜爱的contains选择器来自SizzlejQuery后面的CSS选择器引擎。除非您希望将Sizzle或jQuery物理加载到您的页面中,否则您将无法使用当前的解决方案。

进行基于文本的搜索的唯一真正方法是使用XPath,或查找元素列表(无论如何使用)并在代码中过滤

对于您的基本页面,只需按ID选择即可,只需:

Select select = new Select(driver.FindElement(By.Id("mySelectInput")));

然后您可以使用以下方法选择它:

select.SelectByVisibleText("AT&T");