How to Click a Link in a Table Based on the Text in a Row

how to click a link in a table based on the text in a row

You could locate a cell that contains the specified text, go to the parent row and then find the details link in that row.

Assuming that there might be other detail links you would want to click, I would define a view_details method that accepts the text of the row you want to locate:

class MyPage
include PageObject

table(:grid){ div_element(:class => 'basicGridWrapper')
.table_element(:class => 'basicGridTable') }

def view_details(label)
grid_element.cell_element(:text => /#{label}/)
.parent
.link_element(:text => 'Details')
.click
end

end

You can then click the link with:

page.view_details('DASHBOARD')

how to make a whole row in a table clickable as a link?

Author's note I:

Please look at other answers below, especially ones that do not use jquery.

Author's note II:

Preserved for posterity but surely the wrong approach in 2020. (Was non idiomatic even back in 2017)

Original Answer

You are using Bootstrap which means you are using jQuery :^), so one way to do it is:

<tbody>
<tr class='clickable-row' data-href='url://'>
<td>Blah Blah</td> <td>1234567</td> <td>£158,000</td>
</tr>
</tbody>

jQuery(document).ready(function($) {
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
});

Of course you don't have to use href or switch locations, you can do whatever you like in the click handler function. Read up on jQuery and how to write handlers;

Advantage of using a class over id is that you can apply the solution to multiple rows:

<tbody>
<tr class='clickable-row' data-href='url://link-for-first-row/'>
<td>Blah Blah</td> <td>1234567</td> <td>£158,000</td>
</tr>
<tr class='clickable-row' data-href='url://some-other-link/'>
<td>More money</td> <td>1234567</td> <td>£800,000</td>
</tr>
</tbody>

and your code base doesn't change. The same handler would take care of all the rows.

Another option

You can use Bootstrap jQuery callbacks like this (in a document.ready callback):

$("#container").on('click-row.bs.table', function (e, row, $element) {
window.location = $element.data('href');
});

This has the advantage of not being reset upon table sorting (which happens with the other option).



Note

Since this was posted window.document.location is obsolete (or deprecated at the very least) use window.location instead.

Select a link in a table row based on cell content

Use:

(//td[normalize-space() ="Buenos Dias!"])[1]/following-sibling::td[2]/a[. = 'View']

How to click on a link based on text in a table using selenium

You can use the following codes these codes will handle the dynamic changes.

  1. You can use linkText() method as follows:

driver.findElement(By.linkText("DIY Payroll")).click();


  1. If you want to use xpath then you can use following code.

    driver.findElement(By.xpath(.//a[contains(text(),'DIY Payroll')).click();

If you need any more clarification you are welcome :)

how to click link in a table based on the text in a row protractor

I got the solution. I used xpath for that

element(by.xpath("//span[contains(.,'BG558')]/following::span[contains(.,'Options')][1]").click();

Selenium/Python click on hyperlink in table cell based on value in same row

What about finding span with text T276066 then finding its ancestor tr and then drill-down to the relevant td/a, see the following:

driver.find_element_by_xpath("//span[text()='T276066']/ancestor::tr/td/a[text()='Request ISBN']").click()

Trying to select link text within a table cell if the desired text is also within the cell

As the table is consisted of multiple texts and associated links with same text as Details you can write a method as follows:

def click_me(myString):
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td/span[.='" + myString + "']//following::span[2]"))).click()

Now you can call this method with any of the desired texts to click on the associated link:

click_me("My Desired Text")
# or
click_me("My Text")
# or
click_me("Text")

How to click on a link in a specific row of dynamically loaded table

Assuming the table has a hierarchical structure similar to HTML, you should be able to do the following:

  1. Locate a cell in the row you are looking for that is easy to find, e.g. //WPFDataGridCell[@text='Obama'].
  2. From that cell, move up the hierarchy one step using ...
  3. Now you're in the correct WPFDataGridRow, search down again for the row's "Default" link with //WPFHyperLink[@caption='Default'].

Putting it all together, you'll get a locator like //WPFDataGridCell[@text='Obama']/..//WPFHyperLink[@caption='Default'].

Of course this is only an example based on the information you provided, so if you try it, make sure to pick the attributes with Silk Test's locator spy to make sure you get the correct values.



Related Topics



Leave a reply



Submit