代码之家  ›  专栏  ›  技术社区  ›  Epaga Alex Reynolds

硒元素中的xpath-我做错了什么?

  •  0
  • Epaga Alex Reynolds  · 技术社区  · 15 年前

    我在HTML中有一个页面包含以下代码段:

    ...
    <tbody id="c_scopePane4_tbody">
    <tr id="c_scopePane4_tr_header">
    ...
    </tr>
    </tbody>
    ...
    

    现在我正试图说明 <tr> 在Selenium中使用xpath表达式标记。以下是我的尝试:

     //tr[@id='c_scopePane4_tr_header']
    

    这告诉我

    [error] locator not found: //tr[@id='c_scopePane4_tr_header'], error = Error:
    Element //tr[@id='c_scopePane4_tr_header'] not found 
    

    但如果我将xpath表达式更改为:

     //*[@id='c_scopePane4_tr_header']
    

    …然后就可以了。什么给予?

    2 回复  |  直到 12 年前
        1
  •  3
  •   Dave Hunt    15 年前

    这对我来说也是一样的。也许你的HTML中还有其他东西导致了问题?有多个吗 <tr> (或任何其他元素)具有相同的ID?

    因为ID是(应该是)唯一的,所以您应该能够自信地使用第二个XPath表达式。或者,您可以使用以下命令,但请确保在定位器前面加上 xpath= 因此Selenium知道您使用的定位器类型:

    xpath=id('c_scopePane4_tr_header')
    

    另外,如果您只想选择 <TR & GT; 元素,然后您还可以使用以下选项之一:

    • C砬scopepane4砬tr砬头
    • 标识符=C_Scopepane4_Tr_标题
    • id=c诳scopepane4诳t诳header
        2
  •  0
  •   Nirmal Patel    15 年前

    备用CSS样式定位器:

    css=tr#c_scopePane4_tr_header
    

    或DOM风格:

    dom=document.getElementById("c_scopePane4_tr_header")