9

I want to get the XPATH of an element on a website (my own domain), which I got it using JavaScript code as mentioned in this answer.

Now what I want to click on button which will open a url (cross domain) window and when user click on an element on that window it's XPATH is captured.

I tried doing the same using iframe with no luck.

Now my question is there a way to get the XPATH of an element of another website/ Cross domain?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • I assume you want to do this processing on the client side, not from a server. How about getting the page via a `GET` ajax request, then working on the result? Not sure if I understood your question correctly. Which part of the task is problematic for you? – Mehdi Jul 18 '15 at 20:42
  • Thanks for replying. Getting the page via `GET` will give me the `html` but it will loose the user experience of selecting the element from a page, I updated the question too. – Arpit Aggarwal Jul 19 '15 at 06:20
  • 1
    Can the other domain cooperate? That way you could exchange data using `postMessage` https://w3c.github.io/webmessaging/#dom-window-postmessage between different domains. Otherwise I don't think there is a way with normal client-side script, unless you write a browser extension or plugin. – Martin Honnen Jul 19 '15 at 09:35
  • @MartinHonnen : Yes, other domain is corporate. – Arpit Aggarwal Jul 21 '15 at 05:09
  • I did not ask whether the other domain is a corporate site, rather whether it can cooperate, meaning whether they can set up script on their site to use the `postMessage` method to exchange data with your site. – Martin Honnen Jul 21 '15 at 09:09
  • No, they can not set up the `script`, actually I want to write some solution which will be generic to all websites. E.g. `stackoverflow` might be the one out of it. – Arpit Aggarwal Jul 21 '15 at 09:45

3 Answers3

2

Sorry this is not possible without cooperation from the other (x-domain) site. Browsers are designed not to allow access to the DOM of x-domain documents (iframe included) for security reasons.

If you had cooperation from the other site, they could load your javascript file and then use postmessage to pass the xpath to the original page.

Other options would be to create a bookmarklet users could use on the other page, or a browser extension (Chrome and FF are pretty easy to develop for)... depends on your use case.

Adam Heath
  • 4,703
  • 2
  • 35
  • 50
1

From your comments, I've gathered that you want to capture information from another website that doesn't have Access-Control-Allow-Origin headers that include your domain (e.g. the other site does not have CORS enabled). This is not possible to do cross-domain and client-side due to the Same-Origin Policy implemented in most modern browsers. The Same-Origin Policy prevents any resources on your site from interacting with resources on any other site (unless the other site explicitly shares them with your site using the Access-Control-Allow-Origin HTTP header).

If you want to get information about another site from your site, there is no way around using server-side code. A simple solution would be to implement a server-side proxy that re-serves off-site pages from your own origin, so the Same-Origin Policy will not be violated.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
0

You may get the data using jQuery's load function, and append it to your page.

From there, the DOM nodes from your external page should be accessible for your processing.

$('#where-you-want').load('//example.com body', function() {
    console.log($('#where-you-want'))

    // process the DOM node under `#where-you-want` here with XPath.
})

You can see this in action here: http://jsfiddle.net/xsvkdugo/

P.S.: this assumes you are working with a CORS-enabled site.

Mehdi
  • 7,204
  • 1
  • 32
  • 44