I have a XPath that will select a specific radio button item of my HTML. I would like to enhance my locator to be more dynamic by using a stored variable to represent the text for which my XPath is using to lock on.
This is my current setup
Command: Click
Target:
//*[contains(@for,'Page149.Question48.TypeChoiceOneAnswerRadioButton.holder_ctl02')
and text()="Below 70 – May be too low"]
I would like to replace the literal text "Below 70 – May be too low" with a variable. My proposed Target is failing to evaluate. I hope I just have the syntax incorrect.
Can I do this? Can I use stored variables with XPath locators?
Proposed Target:
//*[contains(@for,'Page149.Question48.TypeChoiceOneAnswerRadioButton.holder_ctl02')
and text()=${radioText}]
EDIT: Adding code. I made a simple example of my HTML radio button list and isolated my Selenium commands. I am using the Selenium IDE in the Firefox. My commands example has comments.
Sample HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<table border="0" class="srvy_radiobuttonlist" id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02">
<tbody>
<tr>
<td>
<input type="radio" checked="checked" value="Page51.Question5.Answer580" name="ctl00$cphSurveyControls$surveyCategoryPage$Page51.Question5.TypeChoiceOneAnswerRadioButton.holder$ctl02"
id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0"><label
for="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0">Male</label>
</td>
<td>
<input type="radio" value="Page51.Question5.Answer581" name="ctl00$cphSurveyControls$surveyCategoryPage$Page51.Question5.TypeChoiceOneAnswerRadioButton.holder$ctl02"
id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_1"><label
for="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_1">Female</label>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Mocked up Selenium IDE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://localhost" />
<title>LocatorTest</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">LocatorTest</td></tr>
</thead><tbody>
<!--Select Male-->
<tr>
<td>click</td>
<td>//*[contains(@id,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0')]</td>
<td></td>
</tr>
<tr>
<td>store</td>
<td>Female</td>
<td>Gender</td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>${Gender}</td>
<td></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>javascript{ storedVars['Gender']}</td>
<td></td>
</tr>
<!--FYI, locates first item in radio collection-->
<tr>
<td>click</td>
<td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02')]</td>
<td></td>
</tr>
<!--Works to select female-->
<tr>
<td>click</td>
<td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and text()='Female']</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and contains(text(),'Female')]</td>
<td></td>
</tr>
<!---- not found -->
<tr>
<td>click</td>
<td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and contains(text(),${Gender})]</td>
<td></td>
</tr>
<!--Works to select female-->
<tr>
<td>click</td>
<td>//*[contains(text(),'Female')]</td>
<td></td>
</tr>
<!---- not found -->
<tr>
<td>click</td>
<td>//*[contains(text(),${Gender})]</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>