简体   繁体   中英

Using variables as selectors with Selenium 2 Webdriver by.cssselector

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM