简体   繁体   中英

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.

<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:

//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. 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.

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"]

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

if you just need member element then use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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