Search code examples
javaseleniumhtml-tablerow

Selecting row from Table - Java Selenium


I have a quick Java table-question. I have a table, containing 4 columns, and X rows. The column's are as follows: Quote Number, Name, Date, and an Add button. I'm trying to, after looking for a specific name in the table, click on the add-button on the row the name was found.

Example:

Quote#  Name       Date        Add-Button

100001  John Doe1  01/01/1980  [Add]
100002  John Doe2  01/01/1981  [Add]
100003  John Doe3  01/01/1982  [Add]
100004  John Doe4  01/01/1983  [Add]

I've written the following code, to identify if a certain name is in the table:

public List<String> getInternetQuoteNames()
    {
        List<WebElement> webName = sh.getElements(newInternetQuotesNames);
        List<String> stringName = new ArrayList<>();
        for(WebElement name : webName)
            stringName.add(name.getText());
        return stringName;
    }

And am now trying to write another method that clicks on the correct [Add]-button, but I'm not sure how to access the correct row as I'm not good with tables. What I wrote so far is:

public void addInternetQuoteByName(String name)
    {
        List<String> listOfNames = getInternetQuoteNames();
        if(listOfNames.contains(name))
        {
            // Find row of provided name
           // Click on [Add]-button on that row
        }
    }

The HTML code for the Table is as follows:

<tbody>
    <tr>
        <td>100040365822</td>
        <td>Test06232016 Test033929</td>
        <td>06/23/2016</td>
        <td>
            <a id="assignQuoteLink_100040365822" class="add_from_pqs" href="#">Add Quote</a>
        </td>
    </tr>
    <tr class="alt">
        <td>100040365772</td>
        <td>Test06232016 Test033723</td>
        <td>06/23/2016</td>
        <td>
            <a id="assignQuoteLink_100040365772" class="add_from_pqs" href="#">Add Quote</a>
        </td>
    </tr>
    <tr>
        <tr class="alt">
            <tr>
                <tr class="alt">
</tbody>

Any tips on how to complete the method and click on the right row's [Add]-button is greatly appreciated!


Solution

  • Give a try to this approach:

    1. Get the size of the table driver.findElements(By.xpath("table xpath")).size()
    2. Iterate over each name record in table and find if the match is found as passed in the input.
    3. Click the add button.

    Code

    public static void addInternetQuoteByName(String Name) 
    {
        WebDriver driver = new FirefoxDriver();
        int rowCount = driver.findElements(By.xpath("xpath of table")).size();
    
        for (int i = 1; i <= rowCount; i++) 
        {
            String sCellValue = driver.findElement(By.xpath("XPATH Of Name row")).getText();
    
            if (sCellValue.equalsIgnoreCase(Name)) 
            {
                 driver.findElement(By.xpath("xpath of add")).click();
            }
        }
    
        driver.close();
    }
    

    xpath for name and add button have to passed as making the tr[i]dynamic for eg: .//*[@id='content']/table/tbody/tr["+i+"]/td[2]

    As I was not sure of exact xpath for name and add button column I have provided an example.

    Let me know if you have issues.