简体   繁体   English

另一个网站/跨域上元素的 Xpath

[英]Xpath of element on another website/cross domain

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.我想获取网站(我自己的域)上元素的XPATH ,我使用答案中提到的JavaScript代码获取了它。

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.现在我想单击将打开一个 url(跨域)窗口的按钮,当用户单击该窗口上的一个元素时,它的XPATH被捕获。

I tried doing the same using iframe with no luck.我尝试使用iframe做同样的事情,但没有运气。

Now my question is there a way to get the XPATH of an element of another website / Cross domain ?现在我的问题是有没有办法获得another website / Cross domain元素的XPATH

Sorry this is not possible without cooperation from the other (x-domain) site.抱歉,如果没有其他(x 域)站点的合作,这是不可能的。 Browsers are designed not to allow access to the DOM of x-domain documents ( iframe included) for security reasons.出于安全原因,浏览器设计为不允许访问 x 域文档(包括iframe )的 DOM。

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.如果您与其他站点合作,他们可以加载您的 javascript 文件,然后使用postmessage将 xpath 传递到原始页面。

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.其他选项是创建一个用户可以在另一个页面上使用的书签,或者一个浏览器扩展程序(Chrome 和 FF 很容易开发)...取决于您的用例。

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 (eg the other site does not have CORS enabled).从您的评论中,我发现您想从另一个没有包含您的域的Access-Control-Allow-Origin标头的网站捕获信息(例如另一个网站没有启用CORS )。 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).同源策略可防止您站点上的任何资源与任何其他站点上的资源交互(除非其他站点使用Access-Control-Allow-Origin HTTP 标头明确与您的站点共享它们)。

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.一个简单的解决方案是实施一个服务器端代理,从您自己的来源重新提供站外页面,因此不会违反同源策略。

You may get the data using jQuery's load function, and append it to your page.您可以使用 jQuery 的加载函数获取数据,并将其附加到您的页面。

From there, the DOM nodes from your external page should be accessible for your processing.从那里,您的外部页面中的 DOM 节点应该可以访问以供您处理。

$('#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/你可以在这里看到这个: http : //jsfiddle.net/xsvkdugo/

PS: this assumes you are working with a CORS -enabled site. PS:这假设您正在使用启用了CORS 的站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM