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">
<fieldset>
<legend>Agenda</legend>
<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>
<div class="fieldHolder2">
<label for="467578">AgendaShowCapacity (1 remaining)</label>
</div>
</li>
<li class="checkboxLeft" data-id="467579" data-z="2">
<div class="fieldHolder1">
<input id="467579" type="checkbox" name="467579" />
</div>
<div class="fieldHolder2">
<label for="467579">AgendaHideReached</label>
</div>
</li>
<li class="checkboxLeft" data-id="467580" data-z="3">
<div class="fieldHolder1">
<input id="467580" type="checkbox" name="467580" />
</div>
<div class="fieldHolder2">
<label for="467580">AgendaShowMessage</label>
</div>
</li>
<li class="checkboxLeft" data-id="467584" data-z="4">
<div class="fieldHolder1">
<input id="467584" type="checkbox" name="467584" />
</div>
<div class="fieldHolder2">
<label for="467584">AgendaWaitlist</label>
</div>
</li>
</ol>
</fieldset>
<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>
</div>
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
(I confirmed this once more in the original .aspx page, so I'm quite sure it is an
, NOT a space).
I tried replacing the first blank with an
, so the xpath changes to //label[text()='AgendaShowCapacity (1 remaining)']
.
However it still doesn't work.
I edit the HTML for that label element with Firebug, replacing
with a space, i.e. changing <label for="467578">AgendaShowCapacity (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
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
in xpath'
//label[text()='AgendaShowCapacity \(1 remaining\)']
?&nbsp;
instead of
.//label[text()='AgendaShowCapacity&nbsp;(1 remaining)']
doesn't work.