Can I pass a C# string variable into an xpath selector? This is for a selenium unit test. I want to do something like this:
string myText = "foo";
Assert.IsTrue(Browser.IsElementPresent("//option[contains(text(), $myText)]"));
Using a string.Format should work:
Assert.IsTrue(Browser.IsElementPresent(string.Format("//option[contains(text(), {0}]",myText)));
yes you can use String.Format
string myText = "foo";
Assert.IsTrue(Browser.IsElementPresent(String.Format("//option[contains(text(), {0})]", myText)));
I see this has already been answered but you could also just do:
Assert.IsTrue(Browser.IsElementPresent(String.Format("//option[contains(text(), " + myText + ")]")));
This works because C# will just append each string to the end of the other.
Thank you for your answers! I was doing a Find in a table, and the above solutions did not work for me, possibly because my string contained hyphens, a decimal point, and numerals like so: "TV-8888-ZIP-54-EN-CTR.05021031"
I assigned the string to the C# variable ID. The answers above failed, I suspect, due to the decimal point and numerals. Using Nashibukasan's answer, I had to explicitly add single quotes. The working Find statement--that gets the value in the third column of the table from the same row that contains the ID string in the first column--ended up looking like this:
value = driver.FindElement(By.XPath(string.Format("//td[contains(text()," + "'" + ID + "'" + ")]/following-sibling::td[2]"))).Text;
Related
Hi all I want to know something regarding to fixed-string in regular expression.
How to represent a fixed-string, regardless of special characters or alphanumeric in C#?
For eg; have a look at the following string:
infinity.world.uk/Members/namelist.aspx?ID=-1&fid=X
The entire string before X will be fixed-string (ie; the whole sentence will appear the same) BUT only X will be the decimal variable.
What I want is that I want to append decimal number X to the fixed string. How to express that in terms of C# regular expression.
Appreciate your help
string fulltext = "inifinity.world.uk/Members/namelist.aspx?ID=-1&fid=" + 10;
if you need to modify existing url, dont use regex, string.Format or string.Replace you get problem with encoding of arguments
Use Uri and HttpUtility instead:
var url = new Uri("http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=X");
var query = HttpUtility.ParseQueryString(url.Query);
query["fid"] = 10.ToString();
var newUrl = url.GetLeftPart(UriPartial.Path) + "?" + query;
result: http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=10
for example, using query["fid"] = "%".ToString(); you correctly generate http://infinity.world.uk/Members/namelist.aspx?ID=-1&fid=%25
demo: https://dotnetfiddle.net/zZ9Y1h
String.Format is one way of replacing token values in a string, if that's what you want. In the example below, the {0} is a token, and String.Format takes the fixedString and replaces the token with the value of myDecimal.
string fixedString = "infinity.world.uk/Members/namelist.aspx?ID=-1&fid={0}";
decimal myDecimal = 1.5d;
string myResultString = string.Format(fixedString, myDecimal.ToString());
I am Searching though the contents of a Excel Spreadsheet template at replacing all the variables represented by #Something.
I have all the variables names come up with code, i'm just looking for a way to replace the them in a string.
Example:
My name is #Name
I want it to become:
My name is John.
My Code explained:
I have an array holding a struct which holds:
TagPointer: this is my variable #name,
TagValue: this would be the name John
Here i can get the index of where my variable is and i have the original String its Comp.
I am just unsure how i can replace the #Something.
int testnum;
if (Comp != "null")
{
if (Comp.IndexOf(ArrayNode[i].TagPointer) != -1)
{
testnum = Comp.IndexOf(ArrayNode[i].TagPointer);
}
}
use string.Format(), string.Replace() there is less convenient
string value = "John";
string result = string.Format("My name is {0}", value);
Unless I'm missing something, can't you use the string.replace function?
You could do something like:
foreach (var tag in ArrayNode)
{
Comp = Comp.Replace(tag.TagPointer, tag.TagValue);
}
Have you ever seen the FormatWith extension?
With it you can write something like this:
Status.Text = "{UserName} last logged in at {LastLoginDate}".FormatWith(user);
Why not using
ie
string name = "Giusepe";
string _tmp = "My name is #Name";
_tmp = _tmp.Replace("#Name", name);
You could iterate over your array and use the .Contains() to see which tag you have present and use the .Replace() to replace the tag with the actual value.
Do you want this:
string a = "#Something";
a = a.Replace("#somethig", Value);
?
Years later, we now have string interpolation to help with this.
I need a help for pattern for float value.
String that i have:
[[-307.,16.01,-171.31],[0.84528,-0.503623,-0.142485,-0.107531],[-1,-2,1,1],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]], [[-306.43,24.47,-176],[0.845282,-0.503624,-0.142472,-0.107528],[-1,-2,1,1],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]]
Pattern that I'm using:
\s*[-+]?([0-9]*\.)?[0-9]*([eE][-+]?[0-9]+)?\s*
What changes that I have to do in my pattern such that I'm able to recognise whole text. Right now problem with
[306.43,24.47,-176] this which is part of this long string.
what changes I have to do with this pattern.
No need for regex. You can use JavaScriptSerializer
var list = new JavaScriptSerializer()
.Deserialize<List<List<List<Double>>>>("[" + yourstr + "]");
It seems you are missing a + or * for the decimal places:
\s*[-+]?([0-9].)?[0-9]+([eE][-+]?[0-9]+)?\s*
I am sure this is a simple question for you guys but I don't know what this developer is doing.
name = String.Format(MyStringBuilder + "");
If I convert this to VB I get the message "operator + is not defined for types system.text.stringbuilder and string". Same thing if I use &.
It looks as if the person who wrote it is attempting to force an implicit conversion of MyStringBuilder to a string using the + operator in conjuction with the empty string.
To perform this assignment in VB you only need:
name = MyStringBuilder.ToString()
In the strictest sense, MyStringBuilder in the original code could be a null instance, at which point making an explicit call to .ToString() would throw an exception. The code sample provided would execute properly, however. In VB, you may want to say
Dim name As String
If MyStringBuilder Is Nothing Then
name = String.Empty
Else
name = MyStringBuilder.ToString()
End If
That doesn't make sense to me because AFAICT passing only one argument to string.format doesn't do anything.
Adding "" to the stringbuilder just coerces it to a string.
name = MyStringBuilder.ToString(); would be how I'd do this in C#. Converting that statement to VB should be loads easier.
Use MyStringBuilder.ToString(). That will fix the problem.
You're trying to concatenate a StringBuilder object and String together - that wouldn't work :)
name = String.Format(MyStringBuilder.ToString() + "");
This should compile correctly.
In VB.NET you would use a "&" instead of a "+"
This line:
name = String.Format(MyStringBuilder + "");
Is causing an implicit cast of MyStringBuilder to string (using the ToString() method) in order to use the "+" operator. It's the same as:
name = String.Format(MyStringBuilder.ToString() + "");
which is the same as
name = MyStringBuilder.ToString();
which becomes this in VB.NET:
name = MyStringBuilder.ToString()
If I write something like this:
string s = #"...."......";
it doesn't work.
If I try this:
string s = #"...\".....";
it doesn't work either.
How can I add a " character to a multi line string declaration in C#?
Try this:
string s = #"..."".....";
The double character usage also works with the characters { and } when you're using string.Format and you want to include a literal instance of either rather than indicate a parameter argument, for example:
string jsString = string.Format(
"var jsonUrls = {{firstUrl: '{0}', secondUrl: '{1}'}};",
firstUrl,
secondUrl
);
string s = "...\"....."; should work
the # disables escapes so if you want to use \" then no # symbol
Personally i think you should go with
string s = string.format("{0}\"{1},"something","something else");
it makes it easier in the long run