2013-07-15 79 views
0

我試圖選擇並打印Watir中動態對象的文本。每次生成對象時,它都是一個隨機字符串。不確定如何指向對象。閱讀Watir動態對象的文本

<dd itemprop="trait"> 
text 
</dd> 

我試過使用xpath並指向itemprop屬性,但程序只是出錯,當我運行它。不知道這是不可能的xpath或如果我的代碼是不正確的。

def read_dd_itemprop(trait) 
    itemprop_text = @@browser.element(:xpath, "//dd[@itemprop='" + trait + "')]").text 
    p itemprop_text 
end 

堆棧跟蹤:

The given selector //dd[@itemprop='trait')] is either invalid or does not result in a WebElement. The following error occurred: 
    InvalidSelectorError: Unable to locate an element with the xpath expression //dd[@itemprop='trait')] because of the following error: 
    [Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/driver_component.js Line: 5773"] (Selenium::WebDriver::Error::InvalidSelectorError) 
    [remote server] file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/driver_component.js:8379:in `FirefoxDriver.annotateInvalidSelectorError_' 
    [remote server] file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/driver_component.js:8410:in `FirefoxDriver.prototype.findElementInternal_' 
    [remote server] file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/driver_component.js:8414:in `FirefoxDriver.prototype.findElement' 
    [remote server] file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/command_processor.js:10421:in `DelayedCommand.prototype.executeInternal_/h' 
    [remote server] file:///var/folders/bv/4z13hkpn095bytggxzrx4l9h0000gp/T/webdriver-profile20130715-7256-ihkq92/extensions/[email protected]/components/command_processor.js:10426:in `DelayedCommand.prototype.executeInternal_' 

我是關閉?這是一個合理的方法,還是應該採取不同的角度?

回答

1

你有一個額外的括號「)」。

itemprop_text = @@browser.element(:xpath, "//dd[@itemprop='" + object + "']").text 

還要注意的是,我期待的XPath是//dd[@itemprop='the value of object'],而不是//dd[@itemprop='object']。請檢查你的object的值是否正確,例如,應該是「特質」。

+0

這是指向特質,我做了一個糟糕的工作,適當地編輯堆棧跟蹤,因爲我現在有點睡眠剝奪。這也可能是這個錯誤的根源,因爲修復工作非常完美。謝謝。 –