Share via


Replace xpath contains text with string value?

Question

Thursday, April 3, 2014 9:05 AM

Hi all,

I am having this problem with replacing the contains text in my xpath with my string value.

This is my code and xpath : FindElement(By.XPath("//*[@id='syi-categories-l1']/ul/li[.='Boeken']")).Click();

My problem is i want to use the value of mystring where 'Boeken' is ,but when i try to replace Boeken with mystring i get just mystring as value instead of the text value of mystring.My other option is to just add SelectedItem.Text instead of mystring but that also does not work for me.

So does anyone know how i can add mystring to the xpath without breaking it?

Extra information :

I am using radDropDownList1.SelectedItem.Text; and that holds the value of my string.

So this will be  string mystring = radDropDownList1.SelectedItem.Text;

What i am tring to do is to get something like this to work  driver.FindElement(By.XPath("//*[@id='syi-categories-l1']/ul/li[.='mystring(here i need the value)']")).Click();

I am using selenium btw.

Regards,

B

All replies (3)

Thursday, April 3, 2014 9:38 AM âś…Answered | 1 vote

Try this:

string xpath = string.Format( "//*[@id='syi-categories-l1']/ul/li[.='{0}']", mystring );
FindElement(By.XPath(xpath)).Click();

Note that mystring needs adjustments if contains apostrophes or other special characters.


Thursday, April 3, 2014 9:22 AM

Try code below :

string myStr = "abc";

mystr = myStr.Replace("abc","def");

jdweng


Thursday, April 3, 2014 9:50 AM

This works perfect thanks a lot!