当id不存在时查找标签文本 [英] Finding label text when id does not exist

查看:106
本文介绍了当id不存在时查找标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Web驱动程序在以下html标签中找到标签文本:

I am using the selenium web driver to find the label text in the below html tags:

<tr id="row" style="display: inline; padding-left: 10px; padding-right: 10px;">
<td>
<table width="776" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="92" height="25" align="left">**Label Text&nbsp**;</td>
<td width="227">
<td width="170" align="left">">**Label Text 2&nbsp**;</td>
</td>
</tr>

我使用driver.findElement(By.id("row")).getText(),但它什么也没找回. 我该如何打印标签文本.谢谢.

I used driver.findElement(By.id("row")).getText(), but it retrieves me nothing. How do I get it to print Label Text. Thanks.

推荐答案

要访问标签文本"单元格,您将需要搜索单元格本身,并且 not 所在行.如果您使用的是WatiN,那就是这种情况. 查找带有标签文本"的任何元素的另一种方法是:

To access the 'Label Text' cells, you will need to search for the cells themselves and not the row that contains them. If you were using WatiN this would be the case. An alternative way of finding any element with 'Label Text' would be:

ReadOnlyCollection<IWebElement> temp = driver.FindElements(By.XPath("//td[@text='Label Text'])); 

如果示例中的标签文本"是不同文本的占位符,这将变得很棘手,您可以尝试在tableRow中抓取包含单元格的每个单元格.但是,只有第一行似乎具有ID.希望这会有所帮助!

If 'Label Text' in your example is a place holder for different text this will become tricky and you could just attempt to grab each cell within the tableRow containing the cells. However, only the first row appears to have an id. Hope this helps!

另外,如果您确实知道想要的单元格并且具有唯一的大小(例如您的示例),则可以使用(非常)hacky解决方案,您可以尝试查找所有单元格,然后手动搜索它们的宽度.例如: foreach(tempCells中的IWebElement temp) { if(temp.Size.Width == 170) temp.Text; }

Alternatively, a (very) hacky solution if you know exactly which cell you want and it has a unique size (like your example) you could try finding all cells and then search through their widths manually. Eg: foreach(IWebElement temp in tempCells) { if(temp.Size.Width == 170) temp.Text; }

这篇关于当id不存在时查找标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆