4

I am writing integration tests for an asp.net mvc website using Selenium Webdriver. I am using C# but answers in C# or Java would be appreciated. I have attached the html table and the generated html code.

I want to click the edit link of groupname "bcde" using webdriver. To click the link, I need the id (2138) & not group name (bcde). I have tried

driver.FindElements(By.XPath("//tr[contains(td[2], 'bcde')]/td[1]").ToList()[0])

but it gives 2137, id of abcde (alphabetically first group containing find string "bcde"). I need the id 2138 of the exact matching groupname "bcde".

enter image description here

<table class="table">
           <tr>
            <th>
                Group id
            </th>
            <th>
                Group Name
            </th>
            <th>

            </th>
    </tr>

        <tr>
            <td>
                2137
            </td>
            <td>
                abcde
            </td>
            <td>
                        <span><a href="/Group/Edit/2137" id="lnkGroupEdit">Edit</a> | </span>
                        <span> <a href="/Instrument?groupID=2137" id="lnkGroupDelete">Delete</a> | </span>
            </td>
        </tr>
        <tr>
            <td>
                2138
            </td>
            <td>
                bcde
            </td>
            <td>
                        <span><a href="/Group/Edit/2138" id="lnkGroupEdit">Edit</a> | </span>
                        <span> <a href="/Delete?groupID=2138" id="lnkGroupDelete">Delete</a> | </span>
             </td>
        </tr>
        <tr>
            <td>
                2139
            </td>
            <td>
                a bcde f
            </td>
            <td>
                        <span><a href="/Group/Edit/2139" id="lnkGroupEdit">Edit</a> | </span>
                        <span> <a href="/Instrument?groupID=2139" id="lnkGroupDelete">Delete</a> | </span>
            </td>
        </tr>
</table>
user2330678
  • 2,221
  • 14
  • 43
  • 67
  • Since u r using findElements to get a list...why don't u try with ToList()[1] for the group id. Unless until ur elements are dynamic. Also can u try out with text()='bcde' or starts-with(.,'bcde')? – Vivek Singh Dec 30 '14 at 19:38
  • @VivekSingh I couldn't make either work. Can you answer the question with complete xpath expressions with text & starts-with? – user2330678 Dec 30 '14 at 19:51

3 Answers3

2

Try this.

//tr/td[contains(.,'2137')]/..//span/a[.='Edit']

Notice the . See my explanation here. .. in xpath allows you to go back and forth of the html hierarchy easily. I used contains because the value of the td has whitespaces

Edit Try this. Seems like it's a wait issue

By byXpath = By.XPath("//tr/td[contains(.,'2137')]/..//span/a[.='Edit']");
new WebDriverWait(Driver,TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(byXpath)).Click();

2nd Edit Most interestingly the a tag that you are trying to click contains respective id. So the best solution of this problem is to use the following xpath as per my understanding

//a[contains(@href,'2138')][.='Edit']
Community
  • 1
  • 1
Saifur
  • 16,081
  • 6
  • 49
  • 73
1

Below fixed it for me.

  driver.FindElement(By.CssSelector("a[href=\"/Group/Edit/" + driver.FindElements(By.XPath("//tr[contains(td[2], 'bcde')]/td[2]")).Where(t => t.Text.Equals(groupName)).ToList()[0].FindElement(By.XPath("..")).FindElement(By.XPath(".//td[1]")).Text + "\"]")).Click();

.FindElement(By.XPath("..")) finds the parent element.

**.//** searches for all child elements. So, FindElement(By.XPath(".//td[1]"))gives the first child of type td.

user2330678
  • 2,221
  • 14
  • 43
  • 67
0

This answer gives us some help.

Finding the exact text match is a little fiddly due to the whitespace surrounding what we see as the text. The XPath function normalize-space() cleans up the whitespace in the element text.

To locate the group name table cell containing exactly "bcde", use the xPath string

//td[normalize-space() = 'bcde']

For the Group Id in the cell associated with that group name cell look at the immediately preceding cell with XPath string

//td[normalize-space() = 'bcde']/preceding-sibling::td

For the edit link we can find it in the following cell with this XPath string

//td[normalize-space() = 'bcde']/following-sibling::td//a[contains(.,'Edit')]
Community
  • 1
  • 1
John O.
  • 708
  • 5
  • 9