0

I'm trying to automate process of pinging a person on a messenger on selecting the messenger name(Nayan)

Please find the Html code

<div class="Presence__text">
<span class="Presence__nameText">Nayan<span class="Presence__contactFlagIndicator"></span>

as text element present inside the span tag is unique (Nayan), i want to select based on that text element.

Problem statement :Unable to select an element using text present in span tag

I wanted to open the text of "Nayan" using xpath, can anyone help me solving this problem please.

1 Answer 1

1

XPath can be used to locate elements based on their text content.
Accordingly to presented here HTML the following XPath can be used:

"//span[contains(text(),'Nayan')]"

Selenium command using this XPath in Java (you didn't mention the language you are using, but accordingly to your previous questions I see you are using Java) can be as following:

driver.findElement(By.xpath("//span[contains(text(),'Nayan')]"));

In case "Nayan" text is unique it's always better to use contains rather to use equals method since web element may contain extra spaces.
I mean when possible [contains(text(),'Nayan')] is better to use than [text()='Nayan']
Also, since you are using Selenium it can be Xpath 1.0 only since Selenium not supporting Xpath 2.0 and higher, it supports XPath 1.0 only

Sign up to request clarification or add additional context in comments.

7 Comments

thanks, tried the above two cases but i got following exception org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //span[contains(text()='Nayan')] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(text()='Nayan')]' is not a valid XPath expression.
It's not what I wrote. //span[contains(text(),'Nayan')]. Not //span[contains(text()='Nayan')]. = is not used with contains
driver.findElement(By.xpath("//span[contains(text(),'Nayan')]")); using this code line iam getting NoSuchElementException @Prophet
Can you share a link to the page you working on? Maybe you are missing a delay? Maybe page need to be scrolled? iframe? many possible issues
actually its a private page, even if i share you wont be able to view it sorry hope you got it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.