2010-06-13 106 views
0

我有我的第20個標籤:如何在Selenium RC中訪問與xpath的非首次匹配?

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 

什麼給?

回答

3

用途:

(//label)[2]

XPath表達式您目前正在使用:

//label[2]

表示:

在文檔中選擇每個label元素,該元素是其父項的第二個子項label。機會是文件中的每個label只是其父項的第一個也是唯一的label子項。在這種情況下,上述表達式不會選擇任何內容

+0

非常感謝! – 2010-06-13 17:38:46