c# - 使用变量作为选择器与 Selenium 2 Webdriver by.cssselector

标签 c# selenium automation webdriver

我正在使用 Visual Studio 2010 在 C# 中编写 Selenium 2 Webdriver 自动化测试。 我到处搜索使用变量作为选择器的示例,但没有找到任何有效的方法。我发现的一个用作选择器的变量示例具有带 $ 前缀并包含在 {} 中的变量。 下面是我正在尝试做的一个例子:

string surveyName = "Selenium test survey";
Driver.FindElement(By.CssSelector("tr[svd='${surveyName}']"))

我得到错误:

OpenQA.Selenium.WebDriverException : Unexpected error. Unable to find element using     css: tr[svd='${surveyName}']

如果我像这样“硬编码”选择器:

Driver.FindElement(By.CssSelector("tr[svd='Selenium test survey']"))

它找到了元素。

svd 是 tr 元素的一个属性。我试图通过此属性的值在表中选择一行。每个测试的文本都不同,因此必须是一个变量。

我尝试过用多种不同的方式来表达变量,但没有成功。任何帮助将不胜感激。

谢谢。

最佳答案

string surveyName = "Selenium test survey";
Driver.FindElement(By.CssSelector(String.Format("tr[svd='{0}']", surveyName))

会做你想做的。这是 C#,所以当它需要一个字符串时,您可以做各种事情来获取该字符串

关于c# - 使用变量作为选择器与 Selenium 2 Webdriver by.cssselector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624125/

相关文章:

Android - 使用 Robotium 进行 UITesting?

Python Mechanize 复选框

c# - 如何在 wpf 中的 ItemsPanelTemplate 中找到控件?

c# - 执行 Ajax 时出现 500 内部服务器错误

c# - 验证将 "Unable to cast object of type ' BaseModel' 抛出到类型 'DerivedModel' 。”

java - 当按钮似乎是从 JavaScript 生成时,如何使用 Selenium 访问按钮

javascript - 通过CSS获取css值并在Selenium中使用

python - Scrapy 在 "__init__"之后不调用任何其他函数

automation - 是否可以在 g1ant 中创建二维数组?

c# - 合流休息 API : Can I create a page by space and parent?