Finding xPath for an element with text

0 votes

I'm trying to use XPath to find an element containing text, but I am not able to get it.

I used the below code:

WebElement searchItemByText = driver.findElement(By.xpath("//*[@id='popover-search']/div/div/ul/li[1]/a/span[contains(text()='Some text')]"));

If I remove the last bit with the "contains" thing, it locates my span element, but I need to select it based on the text contents. It's not gonna be a perfect match of 'Some text' either because it might contain truncated strings as well.

plzz tell me the issue

Jul 31, 2018 in Selenium by darklord
• 6,140 points
650 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I think the problem is here:

[contains(text()='Some text')]

To break this down,

  1. The [] are a conditional that operates on each individual node in that node set -- each span node in your case. It matches if any of the individual nodes it operates to match the conditions inside the brackets.
  2. text() is a selector that matches all of the text nodes that are children of the context node -- it returns a node set.
  3. contains is a function that operates on a string. If it is passed a node set, the node-set is converted into a string by returning the string-value of the node in the node-set that is first in document order.

try to change this to

[text()[contains(.,'Some text')]]

  1. The outer [ ] are a conditional that operates on each individual node in that node set text()is a selector that matches all of the text nodes that are children of the context node -- it returns a node set.

  2. The inner [ ] is a conditional that operates on each node in that node set.

  3. contains is a function that operates on a string. Here it is passed an individual text node (.).

answered Jul 31, 2018 by Meci Matt
• 9,400 points

Related Questions In Selenium

0 votes
1 answer

Need to scroll down for locating an Element with Selenium

There are a couple of options for ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,590 points
40 views
0 votes
1 answer

Finding an element in Selenium

Basically, they are not too different. The ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,030 points
63 views
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 56,100 points
4,395 views
0 votes
1 answer

Searching for the element with   symbol in selenium

Use Below: //div[@class='Tips' and text()='\u00a0'] READ MORE

answered Apr 20, 2018 in Selenium by Vardy
• 2,280 points
916 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
51 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
409 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
40 views
0 votes
1 answer

XPath for the elements with no ID or Name in Selenium Webdriver

Use like this or similar to this: //div[@id="top-level-menu-item-3"]/div[@class="filter-label"] //div[@id="top-level-menu1"] ...READ MORE

answered Apr 17, 2018 in Selenium by Meci Matt
• 9,400 points
739 views
0 votes
1 answer

Is it possible for a website to detect that we are using Selenium with ChromeDriver

Selenium tests for pre-defined javascript variables which ...READ MORE

answered Apr 27, 2018 in Selenium by Meci Matt
• 9,400 points
1,658 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.