2

Say we have the following HTML code

<label for="467578">AgendaShowCapacity (1 remaining)</label>

I tried with the following xpath expression in Chrome but can't locate to that element:

//label[text()='AgendaShowCapacity (1 remaining)']

I was wondering whether the parenthese is to be blamed for this. If it is, how should I deal with it under such situation? Any comments would be appreciated, thanks!

Edit 2012-9-18 real code block posted below

<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&nbsp;(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>
</div>

Here is the essential part of the html page in which I want to locate to that //label element.

Second edit 2012/9/19 Sample C# code using selenium-webdriver to locate that element.

IWebDriver driver = new FirefoxDriver();
IWebElement = driver.FindElement(By.XPath("//div[@id='pageContent']//legend/following-sibling::ol/li/div/label[text()='AgendaShowCapacity (1 remaining)']"));
// Do something to that element
Bruce Sun
  • 631
  • 1
  • 10
  • 26
  • Does `//label` return anything? (In other words: No, the parentheses are not to blame.) – Tomalak Sep 14 '12 at 06:04
  • I've just tested that expression in nokogiri and it works fine. I think you're looking for the problem in the wrong place. – Chris Salzberg Sep 14 '12 at 08:38
  • Perhaps "label" is in a namespace? – Michael Kay Sep 14 '12 at 10:17
  • @Tomalak Yes, "//label" returns every element tagged with "label" on the web page. But why do you ask? – Bruce Sun Sep 17 '12 at 00:54
  • @shioyama Can you give me some detailed explanation on "I think you're looking for the problem in the wrong place"? Thanks! – Bruce Sun Sep 17 '12 at 00:55
  • @MichaelKay No, there's no such namespace on that html page, just confirmed that once more. – Bruce Sun Sep 17 '12 at 01:02
  • @Bruce I'm asking for the same reason as Michael Kay. It's an easy way to confirm that XML namespaces are not the cause. Next question: What does `//label[normalize-space()='AgendaShowCapacity (1 remaining)']` select? – Tomalak Sep 17 '12 at 06:43
  • @Tomalak It returns 'not found'. I don't even know there's such function in xpath as 'normalize-space()'... – Bruce Sun Sep 17 '12 at 07:14
  • @Bruce If there was no such function, you would have received a "no such function" error message. I think it's high time that you show your real HTML code. Letting us *guess* is wasting your time and, more importantly, our's. (Because, if the expression in my last comment did not work, you're not showing your real code.) – Tomalak Sep 17 '12 at 07:24
  • @Tomalak No problem. I just found the poor capability of stackoverflow when inserting a large amount of code, everything get messed up. Can I upload it to github or is there any other way that you can access easily? I'm in China so I don't know what kind of sharing you prefer. Thank you! – Bruce Sun Sep 18 '12 at 00:58
  • The code formatting capabilities of StackOverflow are anything but messed up. The site can handle any amount of code you want to insert, as long as you format it properly. In any case you should not need to paste huge code samples. Just post *the smallest possible sample* (!) that exposes the behavior you describe, instead of every line of code you have. – Tomalak Sep 18 '12 at 06:43
  • @Tomalak I apologize for my impatience. I just found an useful post here [link](http://meta.stackexchange.com/questions/110126/how-can-i-paste-html-markup-into-a-stack-overflow-question-field), then I tried and finally made it to post that block of html code there. Please see my edit above, can you help me locate to that label? Thanks! – Bruce Sun Sep 18 '12 at 08:18
  • @Bruce The non-breaking space is not the same character as the standard space. Your XPath seems to look for a standard space, which means it can never match. Can you also show the part of the code that applies the XPath expression? – Tomalak Sep 18 '12 at 08:42
  • @Tomalak I paste the code from an .aspx page, in chrome (right-click the page then view page source), which I think is the same as html. I'm sorry but I don't quite understand "non-breaking space"? – Bruce Sun Sep 18 '12 at 08:42
  • @Bruce You have ``, but your XPath looks for `AgendaShowCapacity (1 remaining)`, which is not the same thing. – Tomalak Sep 18 '12 at 08:53
  • @Tomalak Yes, I know, but searching "//label[text()='AgendaShowCapacity (1 remaining)']" is also returning "not found". – Bruce Sun Sep 19 '12 at 05:26
  • @Bruce: Of course it does. You have to search for a non-breaking space (char code `160`, hex `A0`). You can't just use any character that looks similar. Searching for `u` when the string contains an `ü` won't work either. So, for example, in JavaScript you can use `\xA0` to get a non-breaking space into a string: `"//label[text()='AgendaShowCapacity\xA0(1 remaining)']"`. Since you chose not to show your code, I leave figuring out how to do it in the language you use to you. PS: Next time, show your code and *actual* input samples right away. Comment cascades like this one help nobody. – Tomalak Sep 19 '12 at 07:20
  • @Tomalak I never chose not to show my code, but I guess what you want to look at is my C# code written for my automation test cases, so see my second edit above. Thanks. – Bruce Sun Sep 19 '12 at 08:06
  • Well, I asked you to share your code 11 comments ago, you did not until right now. You see, your problem is very trivial. Had you shown HTML and C# code right from the start in your question, you would have had three definitive answers 20 minutes after posting it. Instead you chose not to post any code, but your hasty conclusions instead (also see "XY problem"). And here we are, 5 days, 20 comments and 0 answers later, having barely made any progress up until an hour ago. All I'm saying is that the *form* of your question was extremely inefficient, and that you should do better next time. – Tomalak Sep 19 '12 at 08:36
  • @Tomalak Thanks very much for your advice, I'll do it better next time. So do you have any more ideas about that xpath? Since "//label[text()='AgendaShowCapacity\xA0(1 remaining)']" is still not working. – Bruce Sun Sep 20 '12 at 00:39
  • @Tomalak I am somewhat a newbie to stackoverflow, and this is my 5th question posted. I really didn't mean not to post my code out, I just didn't realize to post sample code at that time. Thanks for your advice anyway, I'll remember that as an important rule when asking code specific question next time. – Bruce Sun Sep 20 '12 at 00:47
  • @Bruce The string itself looks fine, the `\xA0` creates a non-breaking space in C# strings as well. I think it's best if you create a new question. Post a small (!) HTML sample (5-10 lines max) and your current C# code (5-10 lines) and ask why you can't seem to match the non-breaking space. If you're dealing with XHTML, don't forget to mention it. If you're using the HTML agility pack or whatever, say so. Keep your question short but complete. If you do that, you'll get an answer almost instantly. Take the above as a learning experience in "how *not* to ask questions on SO". – Tomalak Sep 20 '12 at 06:32
  • @Tomalak Yeah, I'll create a new question after close this one. I really appreciate your patience since you helped me all the way. And I'm also happy that a small misunderstanding is resolved between us. Thank you! – Bruce Sun Sep 20 '12 at 07:45

0 Answers0