Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
954 views
in Technique[技术] by (71.8m points)

selenium - How does dot(.) in xpath to take multiple form in identifying an element and matching a text

I have the below dom structure:

<h3 class="popover-title">
 <div class="popup-title">
   <div class="title-txt">Associated Elements&nbsp;&nbsp(5)</div>
 </div>
</h3>

I am trying to write an xpath which will identify the title "Associated Elements" under h3 tag.

When my xpath is

//div[contains(@class, popover)]//h3[contains(.,'Associated Elements')]

the element is identified.

However when my xpath is

//div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')]

the element is not identified. As per my understanding the dot(.) is a replacement for text(), but then why does it not identify the element when I use the text() function.

However, for another dom structure:

<h3 class="popover-title">
   <a class="btn-popover" href="#">x</a>
   "Associated Elements"
</h3>

The xpath :

//div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')]

&

//div[contains(@class, popover)]//h3[contains(.,'Associated Elements')]

works fine.

Can someone please explain the behaviour of dot(.) under both these scenarios?

Is there a better way to write an xpath that holds good for both the exmaples? Please suggest.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As is tagged so this answer would be based on and the associated XML Path Language (XPath) Version 1.0 specifications.


contains(string, string)

The function boolean contains(string, string) returns true if the first argument string contains the second argument string, and otherwise returns false. As an example:

//h3[contains(.,'Associated Elements')]

Text Nodes

Character data is grouped into text nodes. As much character data as possible is grouped into each text node. The string-value of a text node is the character data. A text node always has at least one character of data. In the below example, text() selects all text node children of the context node:

//h3[text()='Associated Elements']

In your usecase, within the HTML the text Associated Elements &nbsp(5) have &nbsp; which is alternatively referred to as a fixed space or hard space, NBSP (non-breaking space) used in programming to create a space in a line that cannot be broken by word wrap. Within HTML, &nbsp; allows you to create multiple spaces that are visible on a web page and not only in the source code.


Analyzing your code trials

Your first code trial with:

//h3[contains(.,'Associated Elements')]

locates the element as it successfully identifies with partial text Associated Elements

Your second code trial with:

//h3[contains(text(),'Associated Elements')]

fails as the element contains some more characters e.g. &nbsp; in addition to the text Associated Elements.


Reference

You can find a couple of relevant discussions in:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...