1

Cannot find the xpath for span after svg. I need to get list of all elements and validate if the text is Account

I tried xpath

List<WebElement> listOffices = 
driver.findElements(By.xpath("//div  [@id='joblist']//div[@id='view-   
content']//div[@id='page-data']//div[contains(@id,'project-tags')]/span/span/*[name()='svg']"));

The Dom elements are below :

    <div id="project-tags-119651" class="details uk-margin-remove-top" xpath="1">
        <span class="tag mute"> 
            <span uk-icon="icon:tag;ratio:0.8" class="uk-icon">
                <svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="0.8"> <path fill="none" stroke="#000" stroke-width="1.1" d="M17.5,3.71 L17.5,7.72 C17.5,7.96 17.4,8.2 17.21,8.39 L8.39,17.2 C7.99,17.6 7.33,17.6 6.93,17.2 L2.8,13.07 C2.4,12.67 2.4,12.01 2.8,11.61 L11.61,2.8 C11.81,2.6 12.08,2.5 12.34,2.5 L16.19,2.5 C16.52,2.5 16.86,2.63 17.11,2.88 C17.35,3.11 17.48,3.4 17.5,3.71 L17.5,3.71 Z"></path> <circle cx="14" cy="6" r="1"></circle></svg>
            </span> 
            Account
        </span>
    </div>

I need xpath to : Account

supputuri
  • 13,644
  • 2
  • 21
  • 39
BretA
  • 75
  • 8

1 Answers1

1

Here is the xpath that you should use

//span[@class='tag mute' and normalize-space(.)='Account']

Used Class on the safer side, but you have to use normalize-space to remove the preceding and following white spaces (spaces, tabs,new lines) while comparing the text as part of xpath.

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • 1
    By the way, `Account` span is not under `Svg` it's a text node of span with `tag mute` class. – supputuri May 08 '19 at 02:34
  • I could get the text and it is working thanks , to learn i did not understand why does this xpath does not work to get text. //span[@class='tag mute']/span or //span[@class='tag mute']/span/* – BretA May 08 '19 at 03:05
  • 1
    Updated the original post html for better understanding, now if you look at the html `Account` text is directly under span with tag mute class but not `span[]/span` – supputuri May 08 '19 at 03:12
  • Good explanation, +1. @BretA: See also [Testing text() nodes vs string values in XPath](https://stackoverflow.com/q/34593753/290085) – kjhughes May 08 '19 at 04:02