简体   繁体   English

尝试使用来自自定义Firefox扩展/附加组件的xpath和javascript来引用页面中的文本失败

[英]Failing at attempt to reference text within a page using xpath and javascript from a custom Firefox extension/add-on

I have been having issues with this for several days and have been searching all over StackOverflow and Google for the answer. 我已经有几天的问题了,一直在StackOverflow和Google各处搜索答案。 This is code within a Firefox extension which I am referencing a label/text on a webpage. 这是Firefox扩展程序中的代码,我引用的是网页上的标签/文本。 I am trying to get the text from the middle of <b></b> tags. 我正在尝试从<b></b>标记的中间获取文本。 The test is deep within a document so I figured I might be able to use xpath to reference it. 该测试深入文档中,因此我认为我可以使用xpath来引用它。 I have been attempting to use the document.evaluate function to reference this with no luck. 我一直在尝试使用document.evaluate函数来引用它,但是没有运气。 Here is the code snippet that I have most recently tried: 这是我最近尝试过的代码片段:

var result = document.evaluate( '//*[@id="page"]/table[3]/tbody/tr/td[3]/table[1]/tbody/tr[9]/td[2]/b', document, null, XPathResult.STRING_TYPE, null );
alert( 'Value: ' + result.stringvalue );

I am new to DOM and xpath so please let me know if this is at least on the right track. 我是DOM和xpath的新手,所以请让我知道这是否至少在正确的轨道上。 I have tried other methods as well with little success. 我也尝试过其他方法,但收效甚微。 The HTML code I'm trying to pull this out of looks like the following: 我尝试将其删除的HTML代码如下所示:

.
. Rest of the source
.
<b>This is title number 8955592</b>
.
.

I got the xpath from the chrome inspect element feature. 我从chrome inspect元素功能获得了xpath。 I have also tried using firebug to see if anything in there would help as well. 我还尝试使用Firebug来查看其中是否有帮助。

Xpath: Xpath:

//*[@id="page"]/table[3]/tbody/tr/td[3]/table[1]/tbody/tr[9]/td[2]/b

CSSPath: CSSPath:

#page > table:nth-child(3) > tbody > tr > td:nth-child(3) > table:nth-child(3) > tbody > tr:nth-child(9) > td:nth-child(2) > b

My hope is that I have a simple formatting error in my xpath string which I am just starting the string wrong. 我的希望是我的xpath字符串中有一个简单的格式错误,而我刚开始的字符串错误。 This same issue forced me to stop another project I was working on so I decided that I would try and actually address it this time. 同样的问题迫使我停止了我正在从事的另一个项目,因此我决定这次尝试并实际解决这个问题。 I must re-enforce that this is part of a firefox extension I am working on. 我必须强调,这是我正在研究的Firefox扩展的一部分。 I also looked into nodes as a way to do this but that got complicated pretty fast. 我还研究了节点作为实现此目的的一种方法,但是很快就变得很复杂。 Any help is very much appreciated. 很感谢任何形式的帮助。

First, you have 首先,你有

result.stringvalue

which should be 应该是

result.stringValue

(capital V). (大写V)。 However, this should throw an error. 但是,这应该引发错误。 I assume you have checked the console for errors? 我认为您已经检查了控制台中的错误?

Without seeing the page you're trying to extract data from, it's hard to say what's going wrong. 没有看到您要从中提取数据的页面,很难说出问题所在。 But you could narrow it down as follows: 但是您可以按以下方式缩小范围:

var result = document.evaluate(
  '//*[@id="page"]', // /table[3]/tbody/tr/td[3]/table[1]/tbody/tr[9]/td[2]/b',
  document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

What I've done is just take the first part of the XPath expression and comment out the rest for now. 我所做的只是获取XPath表达式的第一部分,然后将其余部分注释掉。 I also asked for a node result type. 我还要求提供节点结果类型。 Then see how many nodes are returned: 然后查看返回了多少个节点:

console.log(result.snapshotLength); // or alert() if you prefer

If the //*[@id="page"] expression yields 1 or more results, then extend the expression and try again: 如果//*[@id="page"]表达式产生1个或多个结果,则扩展该表达式并重试:

 var result = document.evaluate(
  '//*[@id="page"]/table[3]', // /tbody/tr/td[3]/table[1]/tbody/tr[9]/td[2]/b',
  document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

and again check the result.snapshotLength. 并再次检查result.snapshotLength。 When you get a zero for snapshotLength, that's the point where your XPath is failing. 当您将snapshotLength设置为零时,这就是XPath发生故障的地方。 And that should help you figure out why. 那应该可以帮助您找出原因。 But if you can't figure out why, show us those results and we'll see if we can help. 但是,如果您不知道原因,请向我们显示这些结果,我们将看看是否可以提供帮助。

Thank you for your help everyone. 谢谢大家的帮助。 I finally figured this out, I apologize for the late response. 我终于想通了,对于您的迟到表示歉意。 I did misspell stringValue and however I didn't end up using this property. 我确实拼写了stringValue,但是最终没有使用此属性。 I was able to move forward in this and figure it out given the recommendations provided. 我能够在此方面继续前进,并根据所提供的建议加以解决。 I was getting a undefined result initially. 最初我得到的是不确定的结果。 When I took off the .stringValue from the end the result was a XPathResult object. 当我从最后取出.stringValue时,结果是XPathResult对象。 I later determined that you can do a .singleNodeValue on a XPathResult object and get the actual HTMLElement object you are trying to work with. 后来我确定可以对XPathResult对象执行.singleNodeValue并获取要使用的实际HTMLElement对象。 Here is the end result I eventually used to resolve this: 这是我最终用来解决此问题的最终结果:

document.evaluate( '//*[@id="page"]/table[3]/tbody/tr/td[3]/table[1]/tbody/tr[9]/td[2]/b', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue.innerHTML;

Which returns the contents of the <b>text</b> from the page. 从页面返回<b>text</b>的内容。 Even though this may look like a tedious way of getting this result, figuring this out basically taught me how these objects work which I didn't understand when I first asked this question. 即使这看起来可能是获得结果的乏味方法,但弄清楚这一点基本上可以教会我这些对象如何工作,而当我第一次问这个问题时我还是不明白。 Using a systematic way to track if I am on the right path (as recommended) was extremely helpful. 使用系统的方式跟踪我是否在正确的道路上(按照建议)非常有帮助。 Again, thank you very much for your assistance. 再次感谢您的协助。

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

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