Leo
Leo

Reputation: 189

how to find the value in selenium

I am trying to get the value of attribute "value" from the below html. The only value I know is the <b> tag. Name and value keeps on changing based on <b> tag.

<input type="hidden" name="expressions[1].checked"/>
<input type="hidden" value="B" name="expressions[1].expressionLabel"/>
<b>Description</b>
<input type="hidden" value="Description" name="expressions[1].parameterName"/>
<input type="hidden" value="Description" name="expressions[1].fieldName"/>
<input type="hidden" value="RF" name="expressions[1].fieldType"/>
<input type="hidden" value="" name="expressions[1].limitedValues"/>



<input type="hidden" name="expressions[0].checked"/>
<input type="hidden" value="A" name="expressions[0].expressionLabel"/>
<b>Create Date</b>
<input type="hidden" value="StartCreateDate" name="expressions[0].parameterName"/>
<input type="hidden" value="CreateDate" name="expressions[0].fieldName"/>
<input type="hidden" value="RF" name="expressions[0].fieldType"/>
<input type="hidden" value="" name="expressions[0].limitedValues"/>


<tr>
<td colspan="4">
<table width="100%" cellspacing="0" cellpadding="4" border="0">
<tbody>
<tr>
<td class="bb" align="left" colspan="4">
<div id="criteria_table">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<table width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="pad s_pad_right">
<table id="criteria_items" class="table_form_advanced" width="100%" cellspacing="0" cellpadding="4" border="0">
<tbody>
<tr>
<td>
<div>
<div>
<table>
<tbody>
<tr>
<td width="150" valign="top">
<input type="hidden" name="expressions[0].checked"/>
<input type="hidden" value="A" name="expressions[0].expressionLabel"/>
<b>Description</b>
<input type="hidden" value="Description" name="expressions[0].parameterName"/>
<input type="hidden" value="Description" name="expressions[0].fieldName"/>
<input type="hidden" value="RF" name="expressions[0].fieldType"/>
<input type="hidden" value="" name="expressions[0].limitedValues"/>
</td>
<td width="120" valign="top">
<select class="FormSelect" title="Operator" name="expressions[0].operationName">
<option selected="" value="contains">contains</option>
<option value="does not contain">does not contain</option>
</select>
</td>
<td valign="top">
<input class="FormSelect" type="text" title="Description" value="" name="expressions[0].values" maxlength="255" size="20"/>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>

Upvotes: 1

Views: 4591

Answers (3)

autoKarma
autoKarma

Reputation: 839

similar to @lost_boatman's answer, but with some specifics...

In C# (Java will be very similar--it's mostly the capital letters that are different):

string value = driver.FindElement(By.XPath("//b[text() = 'Description']/following-sibling::input[1]")).GetAttribute("value");

expected return: value == "Description"

You could also make the 'input[1]' part of the expression more specific, in case the value from the FIRST input tag isn't always the desired one--say, for example you wanted to get only the 'value' attribute from the input tag containing 'name="expressions[1].fieldType"'. Your expression might look like

string value = driver.FindElement(By.XPath("//b[text() = 'Description']/following-sibling::input[contains(@name, 'fieldType')]")).GetAttribute("value");

expected return: value == "RF"

Upvotes: 4

Nat
Nat

Reputation: 274

If you want to get value of some input element in this case you can use this:

IList<IWebElement> listOfInputs = driver.FindElements(By.TagName("input"));
string value = listOfInputs[index of input you want to check].GetAttribute("value");

Upvotes: 0

lost_boatman
lost_boatman

Reputation: 1

Use the xpath.

The statement will be just like this:

driver.findElement(By.xpath("your elements xpath")).getAttribute("attribute name");

Attribute name will be value for your case.

You can also use driver.findElement(By.name("element_name")).getAttribute("attribute name");

Upvotes: 0

Related Questions