Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:50:07+00:00 2026-06-08T11:50:07+00:00

I have a webpage whose DOM structure I do not know…but i know the

  • 0

I have a webpage whose DOM structure I do not know…but i know the text which i need to find in that particular webpage..so in order to get its xpath what i do is :

doc = Nokogiri::HTML(webpage)
doc.traverse { |node|
  if node.text?
    if node.content == "my text"
      path << node.path
    end
  end
}
puts path

now suppose i get an output like ::

   html/body/div[4]/div[8]/div/div[38]/div/p/text()

so that later on when i access this webpage again i can do this ::

    doc.xpath("#{path[0]}")

instead of traversing the whole DOM tree everytime i want the text

I want to do some further processing , for that i need to know which of the element nodes in the above xpath output have attributes associated with them and what are their attribute values. how would i achieve that? the output that i want is

    #=> output desired
{ p => p_attr_value , div => div_attr_value , div[38] => div[38]_attr_value.....so on }

I am not facing the problem in searching the nodes where “my text” lies.. I wanted to have the full xpath of “my text” node..thts why i did the whole traversal…now after finding the full xpath i want the attributes associated with the each element node that I came across while getting to the “my text” node

constraints are ::I cant use any of the developer tools available in a web browser

PS :: I am newbie in ruby and nokogiri..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:50:08+00:00Added an answer on June 8, 2026 at 11:50 am

    To select all attributes of an element that is selected using the XPath expression someExpr, you need to evaluate a new XPath expression:

    someExpr/@*
    

    where someExpr must be substituted with the real XPath expression used to select the particular element.

    This selects all attributes of all (we assume that’s just one) elements that are selected by the Xpath expression someExpr

    For example, if the element we want is selected by:

    /a/b/c 
    

    then all of its attributes are selected by:

    /a/b/c/@*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage that is transferring A stylesheet as a text/plain MIME type.
C# : I have a string that comes from a webpage's sourcecode: <script type=text/javascript>
I have a GridView on my webpage whose DataSource is a DataTable which is
I have a jQuery Mobile webpage which currently has a width=device-width but my form
I have a text box on a web page, whose value I want to
I have a webpage that when landed on, will detect a user's physical location
I have a webpage that retrieves data (via ajax/php) and shows it in an
I have a webpage where there are few text fields to be filled up
I have a webpage that redirects to another webpage like this: http://www.myOtherServer.com/Sponsor.php?RedirectPage=http://mylink.com/whereIwasgoingtogo.html Then the
I have a webpage that displays a very large list of data. Since this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.