【问题标题】:Cannot locate by CssSelector in Selenium WebDriver无法通过 Selenium WebDriver 中的 CssSelector 定位
【发布时间】:2013-12-31 17:55:04
【问题描述】:

我使用 :contains() 方法,但出现以下错误:

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

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

这是页面的源代码:

@{
    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;
            }
        }
    }

}

【问题讨论】:

  • 如果您可以将 jquery 添加到页面,您可以使用 Browser.ScriptQuery("return $('select').get(0);") 如果您使用 github.com/leblancmeneses/RobustHaven.IntegrationTests 这种方法将让您使用 jquery 链式表达式来获取 IWebElement 或 IWebElement 的集合。 (包括使用查找、过滤、链接)

标签: selenium css-selectors


【解决方案1】:

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

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

进行基于文本的搜索的唯一真正方法是使用 XPath,或者查找元素列表(使用任何你喜欢的方式)并在代码中过滤它们。

对于您的基本页面,只需通过 ID 选择它就很容易了,所以很简单:

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

然后您可以使用以下命令选择它:

select.SelectByVisibleText("AT&T");

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2016-11-11
    • 2011-12-14
    • 2018-10-14
    • 2017-01-23
    相关资源
    最近更新 更多