使用 Selenium 2 Webdriver by.cssselector 使用变量作为选择器
我正在使用 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 元素的属性。我试图通过此属性的值在表中选择一行。每个测试的文本都会不同,因此必须是一个变量。
我尝试过用多种不同的方式表达变量,但没有成功。任何帮助将不胜感激。
谢谢。
I am using Visual Studio 2010 to write Selenium 2 Webdriver automated tests in C#.
I have searched high and low for examples of using variables as selectors and have found nothing that seems to work. The one example I have found of a variable used as a selector had the variable with $ prefix and enclosed in {}.
An example of what I am trying to do is below:
string surveyName = "Selenium test survey";
Driver.FindElement(By.CssSelector("tr[svd='${surveyName}']"))
I get the error:
OpenQA.Selenium.WebDriverException : Unexpected error. Unable to find element using css: tr[svd='${surveyName}']
If I 'hard code' the selector like this:
Driver.FindElement(By.CssSelector("tr[svd='Selenium test survey']"))
it finds the element.
svd is an attribute of the tr element. I am trying to select a row within a table by the value of this attribute. The text will be different for each test and so must be a variable.
I have tried expressing the variable a number of different ways but had no luck making this work. Any help would be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
会做你想做的事。这是 C#,所以当它需要一个字符串时,你可以做各种事情来获取该字符串
will do what you want. This is c# so when it takes a string you can do all kinds of things to get that string