Reputation: 595
I'm using Java Selenium to open links based on the title as input.
I have following part of HTML in which Top 5 Item Exception Types
is title I need to find and its hyperlink <a class="CatalogActionLink" href="javascript:void(null)">Open</a>
is in another <tr><td><span>
tag next to its sibling<tr><td><span>
tag. How can I find element Open
hyperlink by searching title Top 5 Item Exception Types
?
<table cellspacing="4" cellpadding="0" border="0">
<tbody>
<tr>
<td class="CatalogObjectListItemIcon" rowspan="2"><img src="/analytics/res/sk_blafp/catalog/obj_request_b.png" alt="Analysis" title="Analysis"></td>
<td><span class="CatalogObjectListItemTitle">Top 5 Item Exception Types</span>
<span class="CatalogObjectListItemLastModifiedTime">Last Modified 2/15/2018 12:39:45 PM</span>
<span class="CatalogObjectListItemOwner">Owner System Account</span>
</td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><a class="CatalogActionLink" href="javascript:void(null)">Open</a></td>
<td><span class="CatalogActionLinkSeparator"> </span>
</td>
<td><a class="CatalogActionLink" href="javascript:void(null)">Edit</a></td>
<td><span class="CatalogActionLinkSeparator"> </span>
</td>
<td><a class="CatalogActionLink" href="javascript:void(null)" title="Click to see more actions of Top 5 Item Exception Types">More<img border="0" src="/analytics/res/s_FusionFX/uicomponents/obips.UberBar/dropdown_n.png" alt="Dropdown menu">
</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
I can find title using following
WebElement reportElement = driver.findElement(
By.xpath("//span[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"
+ reportName.toLowerCase() + "')]"));
Upvotes: 2
Views: 1312
Reputation: 193088
To identify the WebElement with text e.g. Open, Edit or More, with respect to the WebElement with text as Top 5 Item Exception Types you can write a function which will accept the reference text i.e. Top 5 Item Exception Types and the desired action (i.e either Open, Edit or More) as an input argument and locate the relevant WebElement as follows:
public void clickOperation(String title, String operation)
{
WebElement opsLink = driver.findElement(By.xpath("//td/span[.='" + title + "']//following::tr[1]/td/table/tbody/tr//td/a[@class='CatalogActionLink'][.='" + operation + "']"));
}
You can call the function clickOperation()
along with reference text i.e. Top 5 Item Exception Types and the required operation as a String
an arguments from anywhere within your script to identify the relevant Link WebElement of the respective Top 5 Item Exception Types item as follows :
clickOperation("Top 5 Item Exception Types","Open")
// or
clickOperation("Top 5 Item Exception Types","Edit")
// or
clickOperation("Top 5 Item Exception Types","More")
Upvotes: 1
Reputation: 36
You could try this xpath, it finds your text and then gets back for a few levels, then searches again for specific 'a' element.
//td/span[contains(text(),'Top 5 Item Exception Types')]/../../..//td/a[text()='Open']
Upvotes: 0
Reputation: 52665
Both @Grasshopper and @cruisepandey answers are good (+1). This is just a little shorter version for the same:
//tr[.//span='Top 5 Item Exception Types']/following-sibling::tr//a[.='Open']
Upvotes: 2
Reputation: 29362
try this :
//span[text()='Top 5 Item Exception Types']/ancestor::tr/following-sibling::tr/descendant::a[text()='Open']
Upvotes: 2
Reputation: 9058
Try this xpath -- "//span[.='Top 5 Item Exception Types']//ancestor::tr/following-sibling::tr//a[.='Open']"
Upvotes: 2