XPath Search in Chrome JS Console ("$x(...)") outputs arrays (jQuery Objects?) rather than sections of HTML text (DOM elements?)
Asked Answered
D

2

6

I'm relatively new to using Chrome developer tools/doing XPath searches/this kind of programming in general, so please excuse any incorrect terminology or vague-sounding descriptions. I think the screenshots below will demonstrate what I'm talking about more easily than I can describe with my limited vocabulary.


What I'm Looking For

When I started using developer tools to search XPaths, using the "$x(...)" function would return a section of the html, that I could then navigate within the console or with more specific searches to get a sense of the structure before I extracted text. My console is no longer returning the markup text in this form, but I found an example in another StackOverflow post that will suffice to show what kind of return I'm looking for:

What I would be looking for

I was able to navigate this HTML text within the console (highlighted section) to find the section I needed, before using a more specific XPath, extracting, etc.


What I'm Seeing Now

What I'm now getting instead is an array or nested set of arrays. I think that these are jQuery objects being returned (could be way off here). See below:

What I'm seeing now

I'm sure that in many cases, navigating the elements in this array could actually be easier than working through the HTML and, as someone with very little exposure to web development, I'm sure that there's something I'm missing here. As someone working on a scraping project just looking to find specific elements or text on the page. This is much more difficult to work with.


As a made up example, in the past, if I were trying to pull the names of different kinds of fruit off of a page and I typed into the console...

$x('//*[@class="fruit"]/h1')

...the console would have spit out something like:

[<h1>Peach</h1>, <h1>Strawberry</h1>, <h1>Watermelon</h1>, <h1>Apple</h1>, <h1>Orange</h1>]

And then, had I followed that up with...

$x('//*[@class="fruit"]/h1.text()')

...the console would have spit out:

["Peach", "Strawberry", "Watermelon", "Apple", "Orange"]

Now, if I run a search like this, I'll get a result that will (not literally, but in this form) look something like this:

Array[2]0: form#aspnetForm0: input#__LASTFOCUS1: input#__EVENTTARGET2: input#__EVENTARGUMENT3: input#EktronClientManager4: input#__VIEWSTATE5: input#__VIEWSTATEGENERATOR6: input#__EVENTVALIDATION7: input#ctl00_Header_searchTextBox.searchterm8: input#ctl00_Header_searchButton.searchbtn9: fieldset10: input#ctl00_contentPlaceHolder_login_emailTextBox.max-width11: input#ctl00_contentPlaceHolder_login_passwordTextBox.max-width12: input#ctl00_contentPlaceHolder_login_rememberMeCheckBox13: input#ctl00_contentPlaceHolder_login_signInButton.button.green_events: etc, etc...

For my purposes, that array of strings is much easier to work with, because I can confirm exactly what I'm getting from that XPath search and what I'm going to have to work with once I get the element off the site.


I'm sorry that this post is probably really frustrating to an experienced web programmer, because I'm missing both the larger picture and a lot of the vocabulary. Could someone please explain to me 1. what I going on? Why am I getting these arrays full of data when I was getting the HTML text before and 2. how to revert to getting the HTML text returned when I do this kind of search in the JavaScript console?

Thank you!


Decalescence answered 7/12, 2016 at 20:12 Comment(0)
P
7

There is no difference between the "what I would be looking for" and "what I'm seeing now" screenshots. When you click on the little triangle to the left of "what I would be looking for", you can see that it becomes equivalent to the "what I'm seeing now" format.

For example, opening up DevTools on this very Stack Overflow page, and searching for <h2> elements:

collapsed

When you click on the little triangle to the left of the result you get:

expanded

The $x() function returns a JavaScript array (not jQuery) of DOM nodes that match your XPath query. If there's multiple nodes that match your query, then the result will be an array of multiple nodes.

You can iterate through the results by assigning the results to an array, and then looping through them, like in the screenshot below. I think this is the kind of functionality that you're looking for.

looping through results

Pavo answered 7/12, 2016 at 22:37 Comment(0)
R
0
$x('//*[@class="fruit"]/h1')[0].innerHTML.match(/[\w\s\d]+/gi)?.toString().trim()
Rheinland answered 11/4 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.