开发者

Using variables as selectors with Selenium 2 Webdriver by.cssselector

开发者 https://www.devze.com 2023-02-23 11:32 出处:网络
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开发者_Python百科

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开发者_Python百科 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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号