2013-03-08 36 views
2

當我在Selenium中使用以下XPath表達式時,它工作正常。其發現其中包含在其類屬性特定串的電流元件的祖先DIV:當使用concat()時,爲什麼此Selenium XPath表達式無法返回結果?

inputEl.findElement(By.xpath("ancestor::div[contains(@class, 'x-form-item')]")).getAttribute("class") 

它給是"x-form-item "答案(注意結尾間隔)。

但是,我想祖先精確地具有x-form-item類,而不是其他類如x-form-item-label。所以我改變了如下表達式:

inputEl.findElement(By.xpath("ancestor::div[contains(concat(' ', @class, ' '), ' x-form-item ')]")).getAttribute("class") 

但是,一旦這個到位,Selenium就無法找到元素。它給出了這個錯誤:

org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == ancestor::div[contains(concat(' ', @class, ' '), ' x-form-item ')] (WARNING: The server did not provide any stacktrace information) 

起初我以爲我有某種錯誤,所以爲了簡化,我刪除了前/後空格。據推測,這是語義上是一樣的我的第一個,工作查詢:

inputEl.findElement(By.xpath("ancestor::div[contains(concat('', @class, ''), 'x-form-item')]")).getAttribute("class") 

然而,這也將失敗:

org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == ancestor::div[contains(concat('', @class, ''), 'x-form-item')] (WARNING: The server did not provide any stacktrace information) 

所以我的基本問題是,爲什麼這兩個產生不同的字符串?

  • @class
  • concat('', @class, '')

注:我使用的是IEDriver與IE9

回答

0

爲了達到你想要的,你甚至不需要CONCAT(」」什麼,@class, '')。 您應該能夠使用

By.xpath("ancestor::div[contains(@class, ' x-form-item ')]") 

,因爲你在「@class」標籤尋找「X型項目」,而不是看在「@class」標籤。

+0

不幸的是,我不知道該類是「嵌入」其他類名之間。例如,class屬性可以包含「x-form-item class1 class2」,或者它可以像「class1 class2 x-form-item」,甚至只是「x-form-item」。所以沒有這些會匹配不帶concat的表達式。 – greghmerrill 2013-03-11 20:36:05

+0

你能提供完整的xpath嗎? – Nora 2013-04-16 18:48:01

相關問題