Selenium webdriver通过匹配其他列的文本获取同一行中相邻列的文本 [英] Selenium webdriver get text of adjacent column in same row by matching text from other column
问题描述
我正在使用Selenium Webdriver为asp.net mvc网站编写集成测试。我正在使用C#,但C#或Java中的答案将不胜感激。
我附上了html表和生成的html代码。
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.
我想点击 编辑使用webdriver链接组名 bcde 。要点击该链接,我需要ID(2138)&不是组名(bcde)。
我试过了
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])
但它给出2137,abcde的id(按字母顺序排列的第一组包含查找字符串bcde)。
我需要完全匹配的组名 bcde 的ID 2138。
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".
<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>
推荐答案
下面为我修好了。
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(..))
查找父元素。
**。// **
搜索所有子元素。
所以, FindElement(By.XPath(.// td [1]))
给出第一个 td 的孩子。
**.//**
searches for all child elements.
So, FindElement(By.XPath(".//td[1]"))
gives the first child of type td.
这篇关于Selenium webdriver通过匹配其他列的文本获取同一行中相邻列的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!