简体   繁体   中英

Find XPath of Text which is Under Div Tag and besides span tag

Sorry if this is discussed before. I tried searching it but didn't find exact match. My question is I have low HTML code,

<div class="column1">
<div>
<div class="name">
Dynamic Name
<span class="id" title="ID">Dynamic ID</span>
</div>
</div>
</div>

I am looking for XPath to get text Dynamic Name. Here is what I tried which didn't work

1. //div/div[@class='name'] which is finding text Dynamic Name Dynamic ID

2. //div/span[@class='id']/preceding-sibling::text()

Since Dynamic Name & Dynamic ID, both are the dynamic value, I can't use split & use name as we don't know where to split it. Thanks in advance for your time & help.

This XPath,

normalize-space(//div[@class="name"]/text())

will return "Dynamic Name" for your HTML, as requested.


Thanks but it has some syntax error as i tried putting on Firepath and its giving some error. not sure which one.

Wild guess: See if

//div[@class="name"]/text()

avoids your syntax error (which would be a limitation of your tool as normalize-space() is a proper XPath 1.0 function) and selects your targeted text (although with extraneous whitespace).

由于您指出 XPath //div/div[@class='name']正在工作但返回太多信息,我们可以抓取该元素,抓取innerHTML ,然后在第一个<上拆分,取第一个String ,和trim()以防万一得到答案。

driver.findElement(By.xpath("//div/div[@class='name']")).getAttribute("innerHTML").split("<")[0].trim();

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