Dan Saint-Pierre
Dan Saint-Pierre

Reputation: 25

Read text of dynamic object in Watir

I'm trying to select and print the text of a dynamic object in Watir. Each time the object is generated, it is a random string. Not sure quite how to point to the object.

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

I tried using xpath and pointing to the itemprop attribute, but the program just errors out when I run it. Not sure if this is impossible with xpath or if my code is simply incorrect.

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

Stacktrace:

  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_'

Am I close? is this a reasonable approach, or should I take a different angle?

Upvotes: 0

Views: 409

Answers (1)

Yi Zeng
Yi Zeng

Reputation: 32855

You have an extra bracket ")".

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

Also note that, I was expecting xpath to be //dd[@itemprop='the value of object'], rather than //dd[@itemprop='object']. Please check if you object's value is correct, for example, should be "trait".

Upvotes: 1

Related Questions