如何在Selenium RC中访问与xpath的非首次匹配?

时间:2010-06-13 06:21:19

标签: python xpath selenium selenium-rc

我的页面中有20个标签:

In [85]: sel.get_xpath_count("//label")
Out[85]: u'20'

我可以将第一个默认为:

In [86]: sel.get_text("xpath=//label")
Out[86]: u'First label:'

但是,与我发现的xpath文档不同,我在尝试下标xpath以获取第二个标签的文本时遇到错误:

In [87]: sel.get_text("xpath=//label[2]")
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (216, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (1186, 0))

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)

/Users/me/<ipython console> in <module>()

/Users/me/selenium.pyc in get_text(self, locator)
   1187         'locator' is an element locator
   1188         """
-> 1189         return self.get_string("getText", [locator,])
   1190 
   1191 

/Users/me/selenium.pyc in get_string(self, verb, args)
    217 
    218     def get_string(self, verb, args):
--> 219         result = self.do_command(verb, args)
    220         return result[3:]
    221 

/Users/me/selenium.pyc in do_command(self, verb, args)
    213         #print "Selenium Result: " + repr(data) + "\n\n"

    214         if (not data.startswith('OK')):
--> 215             raise Exception, data
    216         return data
    217 

Exception: ERROR: Element xpath=//label[2] not found

是什么给出了?

1 个答案:

答案 0 :(得分:3)

使用:

<强> (//label)[2]

您当前使用的XPath表达式:

  

<强> //label[2]

表示:

选择文档中的每个label元素,该元素是其父级的第二个label子元素。有可能文档中的每个label都只是其父级的第一个也是唯一的label子级。在这种情况下,上面的表达式不会选择任何内容。