相当于 Firebug 的“复制 XPath";在 Internet Explorer 中? [英] Equivalent of Firebug's "Copy XPath" in Internet Explorer?

查看:16
本文介绍了相当于 Firebug 的“复制 XPath";在 Internet Explorer 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仅限 Internet Explorer 的 Web 应用程序.

I have an Internet Explorer only web application.

我正在探索我们可以做些什么来自动化测试.

I'm exploring what we can do to automate the testing.

Selenium 看起来是个不错的工具,但要能够激活链接等.我需要告诉它它们在哪里.该应用程序在构建时并未考虑到这种测试,因此关键元素上通常没有 id 属性.

Selenium looks like a good tool, but to be able to activate links etc. I need to tell it where they are. The application wasn't built with this kind of testing in mind, so there generally aren't id attributes on the key elements.

没问题,我想,我可以使用 XPath 表达式.但是,如果通过检查页面的源来为按钮找到正确的 XPath,那就太痛苦了.

No problem, I think, I can use XPath expressions. But finding the correct XPath for, say, a button, is a royal pain if done by inspecting the source of the page.

使用 Firefox/Firebug,我可以选择元素,然后使用复制 XPath"来获取表达式.

With Firefox / Firebug, I can select the element then use "Copy XPath" to get the expression.

我有 IE 开发人员工具栏,但它关闭得令人沮丧.我可以单击以选择感兴趣的元素并显示有关它的各种信息.但我看不到任何方便的方法来确定它的 XPath.

I have the IE Developer Toolbar and it's frustratingly close. I can click to select the element of interest and display all sorts of information about it. but I can't see any convenient way of determining the XPath for it.

那么有没有办法用 IE 做到这一点?

So is there any way of doing this with IE?

推荐答案

我会使用书签.我有一个 XPath 相关的,但我不知道它是否适用于 IE.我得走了,但我会测试它,如果它适用于 IE,我会给出它.

I would use bookmarklets. I have one XPath related, but I don't know if it works in IE. I gotta go but I will test it and give it if it works on IE.

我的书签中为 Web 开发人员提供的两个书签站点:Subsimple 的书签Squarefree 的书签.那里有很多有用的东西...

Two bookmarklet sites for Web developers from my bookmarks: Subsimple's bookmarklets and Squarefree's Bookmarklets. Lot of useful things there...

好的,我回来了.我拥有的书签仅适用于 FF,并不是最佳的.我终于重写了它,尽管使用了原来的想法.找不到我找到它的地方了.

OK, I am back. The bookmarklet I had was for FF only, and wasn't optimal. I finally rewrote it, although using ideas from the original one. Can't find back where I found it.

扩展的 JS:

function getNode(node)
{
  var nodeExpr = node.tagName;
  if (nodeExpr == null)  // Eg. node = #text
    return null;
  if (node.id != '')
  {
    nodeExpr += "[@id='" + node.id + "']";
    // We don't really need to go back up to //HTML, since IDs are supposed
    // to be unique, so they are a good starting point.
    return "/" + nodeExpr;
  }
// We don't really need this
//~   if (node.className != '')
//~   {
//~     nodeExpr += "[@class='" + node.className + "']";
//~   }
  // Find rank of node among its type in the parent
  var rank = 1;
  var ps = node.previousSibling;
  while (ps != null)
  {
    if (ps.tagName == node.tagName)
    {
      rank++;
    }
    ps = ps.previousSibling;
  }
  if (rank > 1)
  {
    nodeExpr += '[' + rank + ']';
  }
  else
  {
    // First node of its kind at this level. Are there any others?
    var ns = node.nextSibling;
    while (ns != null)
    {
      if (ns.tagName == node.tagName)
      {
        // Yes, mark it as being the first one
        nodeExpr += '[1]';
        break;
      }
      ns = ns.nextSibling;
    }
  }
  return nodeExpr;
}

var currentNode;
// Standard (?)
if (window.getSelection != undefined) 
  currentNode = window.getSelection().anchorNode;
// IE (if no selection, that's BODY)
else 
  currentNode = document.selection.createRange().parentElement();
if (currentNode == null)
{
  alert("No selection");
  return;
}
var path = [];
// Walk up the Dom
while (currentNode != undefined)
{
  var pe = getNode(currentNode);
  if (pe != null)
  {
    path.push(pe);
    if (pe.indexOf('@id') != -1)
      break;  // Found an ID, no need to go upper, absolute path is OK
  }
  currentNode = currentNode.parentNode;
}
var xpath = "/" + path.reverse().join('/');
alert(xpath);
// Copy to clipboard
// IE
if (window.clipboardData) clipboardData.setData("Text", xpath);
// FF's code to handle clipboard is much more complex 
// and might need to change prefs to allow changing the clipboard content.
// I omit it here as it isn't part of the original request.

您必须选择元素并激活小书签才能获取其 XPath.

You have to select the element and activate the bookmarklet to get its XPath.

现在,书签版本(感谢 Bookmarklet Builder):

Now, the bookmarklet versions (thanks to Bookmarklet Builder):

IE
(我不得不把它分成两部分,因为 IE 不喜欢很长的书签(最大大小取决于 IE 版本!).你必须激活第一个(函数 def)然后是第二个.用 IE6 测试.)

IE
(I had to break it in two parts, because IE doesn't like very long bookmarklets (max size varies depending on IE versions!). You have to activate the first one (function def) then the second one. Tested with IE6.)

javascript:function getNode(node){var nodeExpr=node.tagName;if(!nodeExpr)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}
javascript:function o__o(){var currentNode=document.selection.createRange().parentElement();var path=[];while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');clipboardData.setData("Text", xpath);}o__o();

FF

javascript:function o__o(){function getNode(node){var nodeExpr=node.tagName;if(nodeExpr==null)return null;if(node.id!=''){nodeExpr+="[@id='"+node.id+"']";return "/"+nodeExpr;}var rank=1;var ps=node.previousSibling;while(ps!=null){if(ps.tagName==node.tagName){rank++;}ps=ps.previousSibling;}if(rank>1){nodeExpr+='['+rank+']';}else{var ns=node.nextSibling;while(ns!=null){if(ns.tagName==node.tagName){nodeExpr+='[1]';break;}ns=ns.nextSibling;}}return nodeExpr;}var currentNode=window.getSelection().anchorNode;if(currentNode==null){alert("No selection");return;}var path=[];while(currentNode!=undefined){var pe=getNode(currentNode);if(pe!=null){path.push(pe);if(pe.indexOf('@id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="/"+path.reverse().join('/');alert(xpath);}o__o();

这篇关于相当于 Firebug 的“复制 XPath";在 Internet Explorer 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆