Search code examples
c#seleniumxpathdynamics-crm

get value by Xpath


I'm trying to retrieve a value out of my html by Xpath. For what it's worth, this is a CRM web tool(Microsoft's), and it looks like a dinamic iFrame which changes according to the link selected on its bar(Customers,Appeals etc...)

<tbody><tr class="ms-crm-List-Row" oid="{665A4B9C-9658-E611-922C-441EA1745BC2}" otype="10046" otypename="new_contact_content">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{665A4B9C-9658-E611-922C-441EA1745BC2}" tabindex="0" title="A1" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding"><a href="#" id="gridBodyTable_primaryField_{665A4B9C-9658-E611-922C-441EA1745BC2}_0" target="_self" title="A1" class="ms-crm-List-Link" tabindex="0">A1</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">02/08/2016 12:50</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{675A4B9C-9658-E611-922C-441EA1745BC2}" otype="10046" otypename="new_contact_content">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{675A4B9C-9658-E611-922C-441EA1745BC2}" tabindex="0" title="A1.2" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding"><a href="#" id="gridBodyTable_primaryField_{675A4B9C-9658-E611-922C-441EA1745BC2}_1" target="_self" title="A1.2" class="ms-crm-List-Link" tabindex="0">A2</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">02/08/2016 12:50</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{6B5A4B9C-9658-E611-922C-441EA1745BC2}" otype="10046" otypename="new_contact_content">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{6B5A4B9C-9658-E611-922C-441EA1745BC2}" tabindex="0" title="A5" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding"><a href="#" id="gridBodyTable_primaryField_{6B5A4B9C-9658-E611-922C-441EA1745BC2}_2" target="_self" title="A5" class="ms-crm-List-Link" tabindex="0">A5</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">02/08/2016 12:50</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{6C5A4B9C-9658-E611-922C-441EA1745BC2}" otype="10046" otypename="new_contact_content">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{6C5A4B9C-9658-E611-922C-441EA1745BC2}" tabindex="0" title="A6" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding"><a href="#" id="gridBodyTable_primaryField_{6C5A4B9C-9658-E611-922C-441EA1745BC2}_3" target="_self" title="A6" class="ms-crm-List-Link" tabindex="0">A6</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">02/08/2016 12:50</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{6D5A4B9C-9658-E611-922C-441EA1745BC2}" otype="10046" otypename="new_contact_content">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{6D5A4B9C-9658-E611-922C-441EA1745BC2}" tabindex="0" title="A9" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding"><a href="#" id="gridBodyTable_primaryField_{6D5A4B9C-9658-E611-922C-441EA1745BC2}_4" target="_self" title="A9" class="ms-crm-List-Link" tabindex="0">A9</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">02/08/2016 12:50</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr></tbody>

I'm using :

var nobrValues = _webdriverIE.FindElements(By.XPath("//nobr[@class='gridcellpadding']"));

but the result is 0. meaning no objects found. I also tried:

var _tableOfInterestsCount = _webdriverIE.FindElements(By.XPath("//table[@id='gridBodyTable']/tbody/tr[1]/td"));
        var nobrvalues = var nobrValues = _tableOfInterestsCount.FindElements(By.XPath("//nobr[@class='gridcellpadding']"));

but again, no nobr's found(Count is 0). basically, I would like to get one(or all of them) of the following values out of my html : A1,A1.2,A5,A6,A9.

the table in HTML

<table class="ms-crm-List-Data" cellspacing="0" cellpadding="1" rules="rows" morerecords="0" totalrecordcount="3" allrecordscounted="1" oname="10046" numrecords="3" tabindex="0" primaryfieldname="new_name" summary="foo" border="1" id="gridBodyTable" style="border-style:None;border-collapse:collapse;">
		<colgroup><col width="18px" class="ms-crm-List-CheckBoxColumn"><col width="302" name="new_name" class="ms-crm-List-DataColumn ms-crm-List-SortedColumn"><col width="127" name="createdon" class="ms-crm-List-DataColumn"><col></colgroup><thead><tr class="ms-crm-Hidden-List"><th scope="col" class="ms-crm-Hidden-List"></th><th scope="col" class="ms-crm-Hidden-List">שם</th><th scope="col" class="ms-crm-Hidden-List">created at</th></tr></thead><tbody><tr class="ms-crm-List-Row" oid="{5843AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5843AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="1" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="name1"><a href="#" id="gridBodyTable_primaryField_{5843AB8E-39F7-E611-BE37-00155D47B163}_0" target="_self" title="name 1" class="ms-crm-List-Link" tabindex="0">A1</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding" title="20/02/2017 08:55">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{5943AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5943AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="3" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="A5"><a href="#" id="gridBodyTable_primaryField_{5943AB8E-39F7-E611-BE37-00155D47B163}_1" target="_self" title="3" class="ms-crm-List-Link" tabindex="0">3</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding" title="20/02/2017 08:55">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr><tr class="ms-crm-List-Row" oid="{5A43AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false">
			<td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5A43AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="9" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="9"><a href="#" id="gridBodyTable_primaryField_{5A43AB8E-39F7-E611-BE37-00155D47B163}_2" target="_self" title="9" class="ms-crm-List-Link" tabindex="0">9</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td>
		</tr></tbody>
	</table>


Solution

  • Try the following XPATH for the 1st element (A1):

     var nobrValues = _webdriverIE.FindElement(By.XPath("//tbody/tr[1]/td[2]/nobr/a"));
    

    Or

    If you already know the text i.e., A1 (static content)

    var nobrValues = _webdriverIE.FindElement(By.XPath("//nobr/a[text()='A1']"));
    

    For A2:

    var nobrValues = _webdriverIE.FindElement(By.XPath("//tbody/tr[2]/td[2]/nobr/a"));
    

    Or

    If you already know the text i.e., A2 (static content)

    var nobrValues = _webdriverIE.FindElement(By.XPath("//nobr/a[text()='A2']"));
    

    Please share table HTML code, so that we can make XPATH more specific.