document.evaluate return question

Discussion in 'General Programming Chat' started by Siren, Jun 7, 2013.

  1. Siren

    Siren Level I

    Joined:
    Jun 27, 2011
    Messages:
    81
    Likes Received:
    2
    Quick q:

    Code (Text):
    1.  
    2. if (document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue == "<b>You approach the massive omelette...</b>"){
    3.     console.log("omelette content value found");
    4.     document.forms[1].submit(); //leave
    5. }
    6.  
    when I tested document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null in console, it returned:

    Code (Text):
    1.  <b>You approach the massive omelette...</b>
    however, the == expression keeps returning false when it is blatantly true. I figured the problem was that document.evaluate isn't returning a string of text to compare, so I tried:

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.toString();
    and it returned:

    Code (Text):
    1. "[object HTMLElement]"
    How do I get around this, in order to check the value of the xpath?

    tl;dr can't figure out how to access value of document.evaluate'd xpath

    Thanks!
     
  2. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    The returned code is " <b>You approach the massive omelette...</b>" note the space at the beginning (and possible the end).

    The code you are looking for is "<b>You approach the massive omelette...</b>" with no spaces.

    This could well be your problem
     
  3. Siren

    Siren Level I

    Joined:
    Jun 27, 2011
    Messages:
    81
    Likes Received:
    2
    Thanks for the response!
    Totally my bad, I copied it wrong with the space.

    Console logs on the page, with/without space:

    Without space:
    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue ==  "<b>You approach the massive omelette...</b>"
    2. false
    3.  
    With space:
    Code (Text):
    1.  
    2. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue == " <b>You approach the massive omelette...</b>"
    3. false

    EDIT:

    I read about the parameter for return type of xpath's document.evaluate hoping that changing XPathResult to the appropriate String parameter would work:
    Code (Text):
    1.  
    2. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).singleNodeValue
    3. Error: TYPE_ERR: DOM XPath Exception 52
    XPath Exception 52? What the hell? Googled it,

    What do I do in order to use the result of document.evaluate / make it a string? :-\
     
  4. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    That is because
    Try either:
    Code (Text):
    1.  
    2. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.toString().trim() == "<b>You approach the massive omelette...</b>";
    3.  
    Or

    Code (Text):
    1.  
    2. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).trim() == "<b>You approach the massive omelette...</b>";
    3.  
     
    Siren likes this.
  5. Siren

    Siren Level I

    Joined:
    Jun 27, 2011
    Messages:
    81
    Likes Received:
    2
    Thanks for the help!


    The problem is that any functional code (ie:
    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.toString().trim()
    )

    returns
    Code (Text):
    1. "[object HTMLElement]"
    So that this statement is true:

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.toString().trim() == "[object HTMLElement]"
    2. true
    However, I want the actual value of the HTMLElement.

    Also, for the other one,
    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).trim()
    can't .trim() because it's not a string. if we .toString(); :

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).toString()
    2. "[object XPathResult]"
    and then trim the string result:

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).toString().trim();
    2. "[object XPathResult]"
    we get the same
    Code (Text):
    1. "[object XPathResult]"
    Problem is that we can't use this as a string of text:

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).toString().trim() == "<b>You approach the massive omelette</b>"
    2. false
    3.  
    only comparable this [xpath] object:

    Code (Text):
    1. document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.STRING_TYPE, null).toString().trim() == "[object XPathResult]"
    2. true
    Thanks for the help, guys!