简体   繁体   English

如何使用 XPath 在 div 内使用非标记文本 select div?

[英]How to select div inside div using non tagged text using XPath?

I want to select div with class=membername using xpath with "Laura" as a parameter.我想 select div 与 class=membername 使用 xpath 和“劳拉”作为参数。

<div class="label">
  <div class="membername"></div>
  David
</div>
<div class="label">
  <div class="membername"></div>
  Laura
</div>

This is what I have so far for the XPath:这是迄今为止我对 XPath 的了解:

//div[@class='label']/div[@class='membername']

What comes next after that?之后会发生什么?

EDIT: I forgot to specify that the set of div class="label" is dynamic.编辑:我忘了指定 div class="label" 的集合是动态的。 So there's chance that there will be 5 set of div class="label" and div class="green" will not be the second element with class = membername.所以有可能会有 5 组 div class="label" 和 div class="green" 不会是 class = membername 的第二个元素。

The element is the second one in the indentified list so use the locator:该元素是标识列表中的第二个元素,因此请使用定位器:

(//div[@class="membername"])[2]

so you can use it code as:所以你可以使用它的代码:

print(driver.find_element_by_xpath('(//div[@class="membername"])[2]').text)

Update:更新:

laura is not in the div //div[@class="membername"] it is a text node of //div[@class="label"] laura 不在 div 中//div[@class="membername"]它是//div[@class="label"]的文本节点

 //div[text()[contains(.,"Laura")]]

if you just need member element then use:如果您只需要成员元素,请使用:

//div[text()[contains(.,"Laura")]]/div[@class="membername"]

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

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