Say we have the following HTML code. What I want is to locate to the label element whose text is 'AgendaShowCapacity (1 remaining)' with XPath.

<div id="pageContent">
    <ol class='fieldList custom' data-section='0'>
        <li class="checkboxLeft" data-id="467578" data-z="1">           
            <div class="fieldHolder1">
                <input id="467578" type="checkbox" name="467578" />
            <div class="fieldHolder2">
                <label for="467578">AgendaShowCapacity&nbsp;(1 remaining)</label>
        <li class="checkboxLeft" data-id="467579" data-z="2">           
            <div class="fieldHolder1">
                <input id="467579" type="checkbox" name="467579" />
            <div class="fieldHolder2">
                <label for="467579">AgendaHideReached</label>
        <li class="checkboxLeft" data-id="467580" data-z="3">           
            <div class="fieldHolder1">
                <input id="467580" type="checkbox" name="467580" />
            <div class="fieldHolder2">
                <label for="467580">AgendaShowMessage</label>
        <li class="checkboxLeft" data-id="467584" data-z="4">           
            <div class="fieldHolder1">
                <input id="467584" type="checkbox" name="467584" />
            <div class="fieldHolder2">
                <label for="467584">AgendaWaitlist</label>
<div class="buttonGroup">
    <button type="button" name="ctl00$cph$ctlNavigation$btnAddAnother" class="button" onclick="SubmitForm(this);return false;">Add Another Person</button>
    <span class="textBetweenButtons">or</span>
    <button type="submit" name="ctl00$cph$ctlNavigation$btnContinue" class="button" onclick="SubmitForm(this);return false;">Continue</button>

And here is the sample C# code using selenium-webdriver to locate that element.

IWebDriver driver = new FirefoxDriver();
// Go to the page containing the html code above
IWebElement element = driver.FindElement(By.XPath("//div[@id='pageContent']//legend/following-sibling::ol/li/div/label[text()='AgendaShowCapacity (1 remaining)']"));
// Do something to that element

But this doesn't work, webdriver tells me that it can't find that element using the xpath I provided. So I assumed there's some tricky points (which I can't figure out) with parentheses in that xpath expression:

//div[@id='pageContent']//legend/following-sibling::ol/li/div/label[text()='AgendaShowCapacity (1 remaining)']

Any ideas would be appreciated!

Edit 2012-10-9
Just like user246 said, the key point is &nbsp; (I confirmed this once more in the original .aspx page, so I'm quite sure it is an &nbsp;, NOT a space).
I tried replacing the first blank with an &nbsp;, so the xpath changes to //label[text()='AgendaShowCapacity&nbsp;(1 remaining)'].
However it still doesn't work.
I edit the HTML for that label element with Firebug, replacing &nbsp; with a space, i.e. changing <label for="467578">AgendaShowCapacity&nbsp;(1 remaining)</label> to <label for="467578">AgendaShowCapacity (1 remaining)</label>, this time FirePath found it using xpath //label[text()='AgendaShowCapacity (1 remaining)'].
So I think we can come to a conclusion that &nbsp; is NOT equivalent to a space in xpath.
BTW, I was verifying the xpath with FirePath in Firefox 13.0.1, but I don't think different browser would make any difference since I've also verified in Chrome21 and got the same result.
So this question has turned to 'How to indicate an &nbsp; in xpath'

  • You might have already tried this, but try escaping the parentheses "\ (" and "\ )" (without the space) – Suchit Parikh Oct 8 '12 at 16:07
  • 1
    @SuchitParikh I tried that, but it doesn't work either. Did you mean //label[text()='AgendaShowCapacity \(1 remaining\)']? – Bruce Sun Oct 9 '12 at 2:16
  • Please try &amp;nbsp; instead of &nbsp;. – user246 Oct 9 '12 at 4:17
  • 1
    @user246 //label[text()='AgendaShowCapacity&amp;nbsp;(1 remaining)'] doesn't work. – Bruce Sun Oct 9 '12 at 6:12

Bruce, welcome to SQA. The label in the element in question is AgendaShowCapacity&nbsp;(1 remaining), but the label in your XPath is AgendaShowCapacity (1 remaining). I am not sure a blank and an &nbsp; are equivalent in an XPath. Have you tried replacing that first blank with an &nbsp; in your XPath? Another way to rule out the &nbsp; would be to replace label[text()='AgendaShowCapacity (1 remaining)'] with label[starts-with(text(),'AgendaShowCapacity')].

| improve this answer | |
  • Thanks, user246. I used to ask this question on stackoverflow but got no helpful result. I found SQA filled with so many selenium webdriver questions so this must be the place I'm looking for. – Bruce Sun Oct 9 '12 at 2:19
  • I used to use //label[contains(text(), 'AgendaShowCapacity')] as a workaround, until I notice //label[starts-with(text(), 'AgendaShowCapacity')] in your answer, it is much more accurate to use starts-with than contains. Thank you again! – Bruce Sun Oct 9 '12 at 2:49
  • Is there any more useful xpath function besides starts-with? Can you give me some links where you get to know them? ^_^ – Bruce Sun Oct 9 '12 at 6:31
  • I use w3.org/TR/xpath. – user246 Oct 9 '12 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.