Reputation: 217
I am trying to retrieve for each question i want to answer I want to know the ID and the value.
I have written the following code but I dont know how to finish it so that it gives me the ID and the value:
private IWebElement GetAnswerElement(int questionNumber, int answerNumber)
{
return BrowserFactory.Driver.FindElement(By.XPath(String.Format("//div[@id = 'client-assessment']/div/div/label", questionNumber, answerNumber)));
}
The code looks like this:
<div id="client-assessment" style="overflow: hidden;">
<div class="question">
<label class="question">
Here is my question1
</label>
<div class="radio">
<label>
<input id="xxx" name="xxx" onchange="UpdateAssessment()"
</div>
code continues with more questions
Hope someone can help me out.
Upvotes: 1
Views: 7150
Reputation: 11367
If you want to get the id
and value
attribute of the input
element you can do the following.
private IWebElement GetAnswerElement()
{
var xpath = "//div[@id='client-assessment']/div/div/label/input";
return BrowserFactory.Driver.FindElement(By.XPath(xpath));
}
var element = GetAnswerElement()
var id = element.GetAttribute("id");
var value = element.GetAttribute("value");
Note that I've changed the xpath that it selects the input
element.
If you directly want to use xpath to check for the two attributes you can do the following.
private IWebElement GetAnswerElement(string questionText, string answerText)
{
var xpath = String.Format(
"//div[@id='client-assessment']/div/div/label/input[@id='{0}'][@value='{1}']",
questionText,
answerText);
return BrowserFactory.Driver.FindElement(By.XPath(xpath));
}
var element = GetAnswerElement(
"Product_Contractor_ClientAssessment_Question06",
"Answer1");
Or if you only want to check for the number at the end of the string.
private IWebElement GetAnswerElement(int questionNumber, int answerNumber)
{
var xpath = String.Format(
"//div[@id='client-assessment']/div/div/label/input[ends-with(@id, '{0}')][ends-with(@value, '{1}')]",
questionNumber.ToString("00"),
answerNumber.ToString("0"));
return BrowserFactory.Driver.FindElement(By.XPath(xpath));
}
var element = GetAnswerElement(6, 1);
Note that I format the questionNumber
value with 00
. I do this to prevent the number 66 from beeing matched when 6 is given.
Upvotes: 2