【发布时间】: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 的集合。 (包括使用查找、过滤、链接)