3

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!

6
  • Please show us the HTML code you are working with... Commented Jun 27, 2016 at 17:45
  • 1
    @mo-h, I added the HTML Commented Jun 27, 2016 at 17:57
  • 1
    From identifying what row the name looked for is found on, @mo-h Commented Jun 27, 2016 at 18:06
  • 1
    I call the addInternetQuoteByName("John Doe"); method, and I want to find out which row the name was found one Commented Jun 27, 2016 at 18:07
  • 2
    Why don't just findElement( By.xpath( '//tr/td[contains(.,'John Doe2')]/../a' ).click(); Commented Jun 27, 2016 at 18:07

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

pls ignore the line "driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);"
Edit your answer and remove that line yourself. You should also spend a few minutes properly formatting your code to make it readable.
3

Here's a function to does what you requested. It grabs all the TRs, looks in the 2nd cell (TD) for the name and if found, clicks the "Add Quote" link for that row.

public void addByName(String name)
{
    List<WebElement> rows = driver.findElements(By.tagName("tr"));
    for (WebElement row : rows)
    {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (cells.get(1).getText().equals(name))
        {
            cells.get(3).findElement(By.linkText("Add Quote")).click();
            return;
        }
    }
}

2 Comments

Above code will not work if we have multiple tables in the page! Using tagname is not the best approach.
@prithvi394 True but there wasn't enough info given to specify a table and there was no information given that there was more than one table so ... this works. It can also be easily adapted in rows to only grab the TRs of a specific table. There's a much better/faster way to do this using XPath but there's not much point in posting it now.
0

This requirement sounds a lot like this article. http://www.testautomationguru.com/selenium-webdriver-finding-webelements-using-map/


Basically, you need insert some javascript to create structure as shown here.

{
    "John Doe1": {
        "Add" : []
    },
    "John Doe2": {
        "Add" : []
    }   

}

then get the results returned and type caste to a Map.

Map < String, WebElement >> nameMap = ((JavascriptExecutor) driver).executeScript(queryString);

then your usage would be like

nameMap.get("John Doe3").get("Add").click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.