在Selenium WebDriver中识别元素时出错

时间:2014-05-21 13:08:38

标签: selenium selenium-webdriver

enter image description here

我必须选择上面突出显示的链接New V1 Project,但我无法识别该元素。

我尝试过使用linktext函数,但是它给出了错误......有人可以帮忙吗?

Hierachy就像: -

<tbody>
    <tr>
        <td width="40" align="center">
        <td width="5">
        <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
            <a class="navSelected" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.view&version=1">
        <td width="5">
        <td width="5">
        <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
            <a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=1">
        <td width="5">
        <td width="5">
        <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc">
            <a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=2">
    </tr>
</tbody>

<tbody> <tr> <td width="40" align="center"> <td width="5"> <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc"> <a class="navSelected" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.view&version=1"> <td width="5"> <td width="5"> <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc"> <a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=1"> <td width="5"> <td width="5"> <td align="center" noWrap="" style="border-bottom-color: black; border-bottom-width: 1px; border-bottom-style: solid;" bgColor="#cccccc"> <a class="nav" onmouseover="window.status=''; return true" onmouseout="window.status=''; return true" href="index.cfm?fuseaction=project.create_form&version=2"> </tr> </tbody>

请参阅图片中突出显示的HTML代码...我不确定此元素与Text代码

的关联方式

我用来识别元素的代码是:

a

但上述两种方法都不起作用......请有人帮忙。

2 个答案:

答案 0 :(得分:0)

您可以通过以下xpath找到该链接:

//td/a[normalize-space(text()) = 'New V1 Project']

仅供参考,你的xpath版本几乎是正确的:

  • 无需在@
  • 之前加text()
  • 文字为New V1 Project,而非New v1 Project
  • 您需要使用normalize-space()

希望有所帮助。

答案 1 :(得分:0)

尝试使用cssselector-

 WebElement v1Link = driver.findElement(By.cssSelector("a.nav[href='index.cfm?fuseaction=project.create_form&version=2']"));