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
175 views
in Technique[技术] by (71.8m points)

java - How to write a locator and get both the text present in a single tag in HTML code for selenium automation

This is my html code

<h2>==$0
"How"
 "Are You"
</h2>

Writing locator like //h2/text()[1] for getting "How" text and writing //h2/text()[2] for getting "Are You" text doesn't works for me. Throws the below error

onException - WebDriver error:
<font color= 'red' size='3.5'>org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//h2/text()[2]" is: [object Text]. It should be an element.
question from:https://stackoverflow.com/questions/65948812/how-to-write-a-locator-and-get-both-the-text-present-in-a-single-tag-in-html-cod

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

1 Reply

0 votes
by (71.8m points)

text() doesn't return webelement node but the text content

use instead

//*[text()[contains(.,"Are You")]]

now find the text as

elem.text()

you have to use string operator to extract the specific text you want


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

...