如何在 Selenium RC 中使用 xpath 访问非第一个匹配项?

发布于 2024-09-06 03:23:38 字数 1632 浏览 6 评论 0原文

我的页面中有 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 下标以获取第二个标签的文本时遇到错误:

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

但是,与我找到的 xpath 文档不同,我在 ?

I have 20 labels in my page:

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

And I can get the first one be default:

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

But, unlike the xpath docs I've found, I'm getting an error trying to subscript the xpath to get to the second label's text:

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

What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-09-13 03:23:38

使用:

(//label)[2]

您当前使用的 XPath 表达式:

//标签[2]

表示:

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

Use:

(//label)[2]

The XPath expression you are currently using:

//label[2]

means:

Select every label element in the document that is the second label child of its parent. Chances are that every label in the document is just the first and only label child of its parent. In such a case the above expression selects nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文