简体   繁体   English

如何在xpath中选择具有空文本后代的元素?

[英]How to select elements which have descendant with empty text in xpath?

PAGE - facebook页 - 脸书

I want print all friends online on the facebook (name, link to messenger), but I have a problem.我想在 facebook 上在线打印所有朋友(姓名,到 Messenger 的链接),但我有一个问题。 I don't any idea.我不知道。 How can I get elements which contains two attributes:如何获取包含两个属性的元素:

  1. "//li[@class='_42fz']/a"
  2. "class='_568-'" without text (if div of the class="_568-"/ is without text it means that friend is online). "class='_568-'" without text (if div of the class="_568-"/没有文字,则表示该朋友在线)。

My code is:我的代码是:

List<WebElement> friendElements = driver.findElements(By.xpath("//li[@class='_42fz']/a"));

But I want elements with only friends online.但我想要只有朋友在线的元素。

Example of code:代码示例:

<li id="js_pq" class="_42fz" data-id="101022398278367" aria-controls="js_po" aria-haspopup="true" aria-describedby="js_pp">
<a class="_55ln _qhr" href="https://web.facebook.com/messages/t/107022338465367" rel="ignore">
<div class="_55lp">
<div class="_55lq" aria-hidden="true">
<div class="_5bon">
<div class="_568z">
<div class="_568-">1d</div>
</div>
</div>
<div class="_55lr">Jan Kowalski</div>
<div class="_55ls"/>
</div>
</a>
</li>
<li id="js_pq" class="_42fz" data-id="101012398278367" aria-controls="js_po" aria-haspopup="true" aria-describedby="js_pp">
<a class="_55ln _qhr" href="https://web.facebook.com/messages/t/290222338565367" rel="ignore">
<div class="_55lp">
<div class="_55lq" aria-hidden="true">
<div class="_5bon">
<div class="_568z">
<div class="_568-"></div>
</div>
</div>
<div class="_55lr">Kowalski Jan</div>
<div class="_55ls"/>
</div>
</a>
</li>

And in this case my program must get friend name and href to messenger from the second li, because div of class _568- is without text, it means that this friend is online.在这种情况下,我的程序必须从第二个 li 中获取朋友名称和 href 到 messenger,因为类_568- div 没有文本,这意味着该朋友在线。 If this div has text for example <div class="_568-">1d</div> it means that friend isn't online.如果此 div 具有文本,例如<div class="_568-">1d</div>则表示该朋友不在线。

The html snippet that you pasted is invalid XML, so YMMV , but this should select what you are looking for:您粘贴的 html 片段是无效的 XML,因此YMMV ,但这应该选择您要查找的内容:

//li[@class='_42fz']//div[@class='_568-'][not(text())]/../../../../../a/@href

or more elegant:或更优雅:

//li[@class='_42fz'][descendant::div[@class='_568-'][not(text())]]/a/@href

DEMO 演示

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

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