Forum Discussion

enriquebravo's avatar
enriquebravo
Contributor
2 years ago
Solved

Store selector value

Is it possible to store the selector's xpath values to a variable? I am in a situation that I need to extract and later use the "967" value from this xpath:

 

//*[@id="sectionContent-967"]/div/slot/records-record-layout-row[5]/slot/records-record-layout-item[1]/div/div/div[2]

 

Thanks.

  • Hi enriquebravo 

     

    Not sure if you have a dom object and/or node list, which has functions to get a node attribute, so I will assume all you have is a string to parse out the value.

     

    This is a generic function that you can supply the desired string, two conditions that delineate/outline your search text, where to start looking on the desired string, and whether to consider case for the start and end strings.  Please note that this does not provide any error trapping.

     

      function stringParser(stringToSearch, startSearchString, endSearchString, startPosition, caseSensitive)
      {
        if (startPosition == null)
          startPosition = 0;
          
        if (caseSensitive == null)
          caseSensitive = false;
          
        var indexStartPosition = aqString.Find(stringToSearch, startSearchString, startPosition, false) + startSearchString.length;
        var indexEndPosition = aqString.Find(stringToSearch, endSearchString, indexStartPosition, false);
        
        return aqString.SubString(stringToSearch, indexStartPosition, indexEndPosition indexStartPosition);
      }

     

    In your case you would supply the xPath as the stringToSearch, something like sectionContent- or Conent- and “]/ to delineate the number for the startSearchString and endSearchString.  You can specify where to start on the string, zero is fine, and if you want it to be case sensitive (true) or not (false). 

     

    function test()
    {
      var xPath = "//*[@id=\"sectionContent-967\"]/div/slot/records-record-layout-row[5]/slot/records-record-layout-item[1]/div/div/div[2]";
    
      var value = stringParser(xPath, "sectionContent-", "\"]/", 0, false);
      // returns 967
    }

     

    I hope this helps.

     

    Regards

     

2 Replies

  • Hi enriquebravo 

     

    Not sure if you have a dom object and/or node list, which has functions to get a node attribute, so I will assume all you have is a string to parse out the value.

     

    This is a generic function that you can supply the desired string, two conditions that delineate/outline your search text, where to start looking on the desired string, and whether to consider case for the start and end strings.  Please note that this does not provide any error trapping.

     

      function stringParser(stringToSearch, startSearchString, endSearchString, startPosition, caseSensitive)
      {
        if (startPosition == null)
          startPosition = 0;
          
        if (caseSensitive == null)
          caseSensitive = false;
          
        var indexStartPosition = aqString.Find(stringToSearch, startSearchString, startPosition, false) + startSearchString.length;
        var indexEndPosition = aqString.Find(stringToSearch, endSearchString, indexStartPosition, false);
        
        return aqString.SubString(stringToSearch, indexStartPosition, indexEndPosition indexStartPosition);
      }

     

    In your case you would supply the xPath as the stringToSearch, something like sectionContent- or Conent- and “]/ to delineate the number for the startSearchString and endSearchString.  You can specify where to start on the string, zero is fine, and if you want it to be case sensitive (true) or not (false). 

     

    function test()
    {
      var xPath = "//*[@id=\"sectionContent-967\"]/div/slot/records-record-layout-row[5]/slot/records-record-layout-item[1]/div/div/div[2]";
    
      var value = stringParser(xPath, "sectionContent-", "\"]/", 0, false);
      // returns 967
    }

     

    I hope this helps.

     

    Regards

     

    • enriquebravo's avatar
      enriquebravo
      Contributor

      All I had was the string, so this function came in very handy. I will use it definitely for other string searches as well.

       

      I appreciate it very much. Have a nice day!