curiousToKnow
curiousToKnow

Reputation: 79

How to find xpath from javascript href

The scenario is I need to check all the by default elements present on a web page. I am trying to check the headers available on a web table. I am able to find all headers except one whose name is IP Address / Host Name

<div id="ctl00_ContentPlaceHolder1_RadGrid1_GridHeader" class="rgHeaderDiv" style="overflow: hidden;">
<table id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_Header" class="rgMasterTable rgClipCells rgClipCells" style="border-color:#6788BE;border-width:1px;border-style:solid;width:100%;table-layout:fixed;overflow:hidden;empty-cells:show;">
<colgroup>
<thead>
<tr>
<th class="rgHeader rgSorted" style="font-weight:bold;font-style:normal;text-decoration:none;text-align:left;" scope="col">
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl05','')" title="Click here to sort" onclick="Telerik.Web.UI.Grid.Sort($find('ctl00_ContentPlaceHolder1_RadGrid1_ctl00'), 'HostIP'); return false;">IP Address / Host Name</a>
<input class="rgSortAsc" type="button" title="Sorted asc" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl06','')" value=" " name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl06">
</th>
</tr>
</thead>
<tbody style="display:none;">
</table>
</div>

I tried finding using below codes but failed to do so. driver.findElement(By.xpath("//div[@id='ctl00_ContentPlaceHolder1_RadGrid1_GridHeader']//a[contains(@href,'IP Address / Host Name')]"));

driver.findElement(By.xpath("//a[contains(@href,'IP Address')]"));

driver.findElement(By.linkText(IP Address / Host Name));

I tried with changing div id and its value ti table id and corresponding value too but script fail saying not able to find element with xpath...

My script always says failed to find element with id... or xpath... Please help.

Upvotes: 1

Views: 1030

Answers (1)

Travis
Travis

Reputation: 2058

If you're having trouble accessing the element programmatically, my advice would be to use the Selenium IDE to select the element, then export the test case by going to File > Export Test Case As... > Java / JUnit 4 / WebDriver.

I tried this myself and the resulting code uses:

driver.findElement(By.linkText("IP Address / Host Name"))

It seems to work fine in the Selenium IDE. I haven't tested the resulting Java code. - From your syntax, it appears you're using Java.

You say you've tried using By.linkText(), so maybe there's a bigger problem? (I notice you don't have quotes around your By.linkText() example, but I assume this is a typo.)

Upvotes: 1

Related Questions