我想访问span
标记的集合,其中value attribute
如下所示
<span class="label_xmlvalue_row value" value="Automation_TestBundle_13thMarch">
<iframe>
<html>
<head></head>
<body>
<table>
<tbody>
<tr class="empty">there are no items in this view</tr>
<tr>
<td>
<span class="bigger value">
<span class="text">3. Author</span>
</span>
</td>
<td>
<span class="label_xmlvalue_row value" value="Automation_TestBundle_13thMarch">
<span class="text" title="Task Name: Automation_TestBundle_13thMarch">Task Name:
<span class="xmlvalue">Automation_TestBundle_13thMarch</span>
</span>
</span>
</td>
<td>
<span class="bigger value">
<span class="text">3. Author</span>
</td>
</tr>
<tr>
<td>
<span class="bigger value">
<span class="text">Reviewer</span>
</span>
</td>
<td>
<span class="label_xmlvalue_row value" value="TestBundle_13thMarch">
<span class="text" title="Task Name: TestBundle_13thMarch">Task Name:
<span class="xmlvalue">TestBundle_13thMarch</span>
</span>
</span>
</td>
<td>
<span class="bigger value">
<span class="text">3. Author</span>
</td>
</tr>
<tr>.....</tr>
</tbody>
</table>
</body>
</html>
</iframe>
所以我尝试了一些事情并提醒你注意
1. driver.switchTo.Frame(element);
var ele = driver.FindElements(By.TagName("span"));
ele.Text property gives "Cannot access dead object"
2. driver.switchTo.Frame(element);
IWebElement ele = driver.FindElements(By.TagName("span")).Where(x=>x.GetAttribute("Value").Equals("Automation_TestBundle_13thMarch")).First();
Exception thrown - Invalid Operation Exception
Inner Exception = null
Message = can't access dead object
1.我们可以直接访问span标签而无需找到html元素(/html/body/table/tbody/tr/td
)吗?
如果意味着在这种情况下为什么会抛出异常
如果没有,请给我一个替代解决方案。
提前致谢。
答案 0 :(得分:0)
由于您只有一个框架,因此您可以使用: -
driver.switchTo.Frame(0);
您可以尝试使用以下xpath
//span/@value
代码:
ReadOnlyCollection<IWebElement> links = driver.FindElements(By.XPath("//span/@value"));
foreach(IWebElement link in links)
{
string text = link.Text;
Console.WriteLine(text);
}
希望它会对你有所帮助:)。
答案 1 :(得分:0)
首先启用iframe
Driver.switchto.iframe(iframe path);
WebElement headobject=DriverObj.findElement(By.xpath("//head/body/table/tbody//td"));
List<WebElement> spanobject=headobject.findElements(By.tagName("span"));
system.out.printf(spanobject.size());
Iterator<WebElement> span=spanobject.iterator();
while(span.hasNext()){
WebElement span1=span.next();
String spangettext=span1.getText();
我在这里执行获取文本的操作 你按照自己的意愿行动
单击
span1.click();
答案 2 :(得分:0)
感谢您的支持,我得到了解决方案,
IWebElement elem = driver.FindElements(By.TagName("tr")).Where(x => x.Text.ToLower().Contains("Automation_TestBundle_13thMarch".ToLower())).First();
我只是查看span标记的文本,而不是获取属性值,因为如果找不到该属性,GetAttribute()
方法抛出空引用异常