位于第二个br标签内的文本内容无法被打印。

huangapple go评论106阅读模式
英文:

text content located in second br tag can't be printed

问题

我试图通过以下XPath打印出位于第二个br标签中的文本内容,但所有br标签中的文本都被打印在控制台上。可能的原因是什么?

driver.findElement(By.xpath("//*[text()[contains(.,'Telefon')]]")).getText();

位于第二个br标签内的文本内容无法被打印。

英文:

I'm trying to print the text content located in second br tag by following xpath but all texts which are in all br tags are printed in console. What might be the reason ?

driver.findElement(By.xpath("//*[text()[contains(.,'Telefon')]]")).getText();

位于第二个br标签内的文本内容无法被打印。

答案1

得分: 0

driver.findElement()函数返回具有给定XPath的所有元素。要在Selenium中仅获取一个元素,您可以使用driver.find_element_by_xpath(fxp)函数,其中'fxp'是给定元素的完整XPath

英文:

The driver.findElement() function returns all the elements with the given Xpath. To get only one element in selenium you can use driver.find_element_by_xpath(fxp) function, where 'fxp' is full XPath of the given element.

答案2

得分: 0

尝试将您的 XPath 表达式更改为

//h2/br/following-sibling::text()[contains(.,'电话')]

看看是否有效。

英文:

Try changing your xpath expression to

//h2/br/following-sibling::text()[contains(.,'Telefon')]

and see if it works.

答案3

得分: 0

以下是翻译好的内容:

你无法获取文本的原因是因为文本不在 br 标签内。

< 表示开启 br 标签,/> 表示闭合。

此外,如果你多了解一点,甚至 / > 也是多余的。如果只有 <br>,文本不会包含在其中,因为:

<br> 标签是一个空标签,这意味着它没有结束标签。

重点是,所有的文本都在 h2 中。你需要尽力处理这个问题。

要解决你的问题,你需要:

  • .getAttribute("innerHTML") - 这将给你带有 br 标签的 h2 的所有文本
  • 在字符串上使用字符串 <br> 进行分割(请注意,在 Chrome 中我的 <br /> 变成了 <br> - 你可能需要调整这个
  • 选择要么选择 item[2],要么使用 lambda 表达式选择包含你的文本的项目(选择你感觉更舒服的方式)

这些步骤看起来是这样的:

//获取元素
var h2Element = driver.findElement(By.xpath("// *[text()[contains(.,'电话')]]"));
var myTextArray = h2Element.getAttribute("innerHTML").split("<br>");

//方法1 - 仅打印 [1] 项
var approach1Text = myTextArray[2];
System.out.println(approach1Text);

//方法2 - 使用 lambda 表达式选择包含的内容
var approach2Text = Arrays.stream(myTextArray).filter(a -> a.contains("电话")).findFirst().get();
System.out.println(approach2Text);

额外说明一下 - 你可能在让 xpath 正常工作时遇到了一些问题,因为 br 标签将文本拆分为单独的元素。因此,你的 h2 实际上有多个 text() 值。它有 text()text()[2]text()[3],等等 - 有多少个 br 就有多少个。

我为你制作了一个简单的页面来测试这个问题 - 只是为了向你展示发生了什么:(注意开发工具中的 xpath)

位于第二个br标签内的文本内容无法被打印。

这是 text()[3],因为 xpath 从 1 开始编号(与上面的从 0 开始的 Java 代码相比)。然而 - 那只是一个例子,说明了为什么它很棘手,我不建议你以这种方式操作

消除 &lt;br&gt;(以及其他标签!)对文本的影响的简单方法是使用 normalize-space()

像这样的 xpath 是有效的,并且相对简单易懂。

//h2[contains(normalize-space(),'电话')]

在我的示例页面上也可以运行:

位于第二个br标签内的文本内容无法被打印。

我分享这个额外的内容,以防你还有其他需要分割文本的对象,希望它对你以后有所帮助。

...总之,很高兴你能让原始的 xpath 正常工作。那也很好。

英文:

The reason you can't get the text is because the text is not in the br tag.

&lt; open br tag /&gt; close

Additionally, if you read a bit more about it, even the /&gt; is surplus to requirements. If you had just &lt;br&gt; the text wouldn't be contained within it because:

> The &lt;br&gt; tag is an empty tag which means that it has no end tag.

The point is, all your text is h2. You need to deal with that the best you can.

To solve you're issue you'll need to:

  • .getAttribute(&quot;innerHTML&quot;) - this will give you all the text of the h2 with the br tag
  • split your string on the string &lt;br&gt; (please note that in chrome my &lt;br /&gt; becomes &lt;br&gt; - you might need to adjust this)
  • select either select item[2] or do a lamda to select the item that contains your text (do whichever you feel more comfortable with)

And those steps look like this:

        //Get the element, 
        var h2Element = driver.findElement(By.xpath(&quot;//*[text()[contains(.,&#39;Telefon&#39;)]]&quot;));
        var myTextArray = h2Element.getAttribute(&quot;innerHTML&quot;).split(&quot;&lt;br&gt;&quot;);

        //approach 1 - just print the [1] item
        var approach1Text = myTextArray[2];
        System.out.println(approach1Text);

        //aproach 2 - use a lamda to select by contains
        var approach2Text = Arrays.stream(myTextArray).filter(a -&gt; a.contains(&quot;Telefon&quot;)).findFirst().get();
        System.out.println(approach2Text);

For a bonus note - you probably had fun getting your xpath to work because the br tag splits the text into separate elements. As result your h2 actually has multiple text() values. It has text(), text()[2], text()[3], etc - as many as there are brs

I put together a simple page to test this for you - just to show you what's going on: (note the xpath in dev tools)

位于第二个br标签内的文本内容无法被打印。

This is text()[3] because xpaths are indexed from 1 (comapred to the java code above that starts at 0). However - that's just an example of why it's tricky, i wouldn't recommend you do it that way.

The easy way to eliminate the &lt;br&gt; (and other tags!) affect on text is to use normalize-space().

An xpath like this works and is realtively simple to follow.

//h2[contains(normalize-space(),&#39;Telefon&#39;)]

Maps to my sample page OK:

位于第二个br标签内的文本内容无法被打印。

I share this extra bit in case you have any more text-split objects and it helps you down the line.

...All that said - good work on getting your original xpath to work. That's good too.

huangapple
  • 本文由 发表于 2020年8月29日 23:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63649002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定