Maya
Maya

Reputation: 1

Handle with tables in selenium C#

I a trying to handle with tables in selenium C#. I have a table which has a few rows and a few columns. Each row has column with name and buttons. I want to click "Open" button, where the name in the row is, for example "Name1". But i can't find even the td's in the table. Here's what i was trying to do:

public void SelectNameAndClick(string name)
        {
            IWebElement tableElement = driver.FindElement(By.XPath("//table[@id='TableMp']"));
            IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("tr"));
            IList<IWebElement> rowTD;
            foreach (IWebElement row in tableRow)
            {
                if (row.Text.Equals(name));
                rowTD = row.FindElements(By.TagName("td"));

I attached screen shot of the table: here

<table class="table table-condensed table-hover" id="TableMp" style="" data-bind="visible: opAGents().length > 0">
                
                    <tbody data-bind="foreach: opAGents">
                        <tr>
                            <td>
                                <span  agentid="OPAgents/4b2f885101f740fdb72c5874596577ac" data-original-title="4b2f885101f740fdb72c5874596577ac">4b2f885101f740fdb72c5874596577ac</span>
                            </td>
                            <td class="vert-align">
                                <span >Jun 23, 2020 9:07 PM</span>
                            </td>

                            <td class="vert-align">
                                <span data-original-title="4b2f885101f740fdb72c5874596577ac">4b2f...</span>
                                <button class="btn btn-default btn-aligned-left" title="" data-original-title="Copy token"></i></button>
                            </td>
                            <td>
                                <span  class="agent-led agent-active" data-original-title="Connected"></span>
                            </td>

                            <td class="vert-align hidden-xs">
                                
                                <button data-bind="class="btn-aligned-left quick-action btn btn-sm btn-primary">
                                    <span class="no-pointer-events">
                                        <span>Open</span>
                                    </span>
                                </button>
                            </td>
                            <td class="vert-align">
                                <button class="btn btn-default btn-aligned-left" ></i></button>
                            </td>
                        </tr>
                    
                        <tr>
                            <td class="vert-align">
                                <span  agentid="OPAgents/46414e7df0234d01aa6df6df7c195d17" data-original-title="">Name1</span>
                            </td>
                            <td class="vert-align">
                                <span >Jul 23, 2020 10:17 AM</span>
                            </td>

                            <td class="vert-align" style="white-space:nowrap">
                                <span data-original-title="46414e7df0234d01aa6df6df7c195d17">4641...</span>
                                <button class="btn btn-default btn-aligned-left"><i class="fa fa-files-o" data-bind="attr:{ 'data-original-title': 'Copy token' }, toolTip: { hideOnMobile: true }" title="" data-original-title="Copy token"></i></button>
                            </td>
                            <td>
                                <span class="agent-led agent-active" data-original-title="Connected"></span>
                            </td>

                            <td class="vert-align hidden-xs">
                                
                                <button  class="btn-aligned-left quick-action btn btn-sm btn-primary">
                                    <span class="no-pointer-events">
                                        <span>Open</span>
                                    </span>
                                </button>
                            </td>

Upvotes: 0

Views: 458

Answers (1)

msmolcic
msmolcic

Reputation: 6577

This should work for you:

public void SelectNameAndClick(string name)
{
    // Selecting your table element.
    IWebElement table = driver.FindElement(By.XPath("//table[@id='TableMp']"));

    // Select the row where name of the agent equals to the name we're searching for.
    IWebElement row = table
                .FindElements(By.TagName("tr"))
                .FirstOrDefault(element =>
                    element.FindElements(
                        By.XPath($"//span[@agentid and text()='{name}']")).Count > 0);

    // If there is no such row, do nothing.
    if (row == null)
        return;

    // Find the button with the 'Open' text somewhere within it and click the button.
    IWebElement button = row.FindElement(By.XPath("//button//*[text()='Open']"));
    button.Click();
}

Upvotes: 1

Related Questions