Finding elements by content containing  

6 posts, 1 answers
  1. kovyar
    kovyar avatar
    42 posts
    Member since:
    Jul 2010

    Posted 19 Aug 2010 Link to this post

    Hi all.

    As we have to support multi-browser automated tests, the tests must run in both IE and Firefox.
    After installing the latest build of WebAii we finally can work fine with both of them.
    Still I suspect a funny problem with these browsers because they render non-breakable space ( ) in different manner.
    Just have a look at this piece of HTML code:

    <a href="http://google.com">mega&nbsp;link</a>

    Looks usual.

    But now, if I want to find this link by the text it contains, troubles appear.

    Settings settings = new Settings(BrowserType.InternetExplorer, "d:\\log\\");
    Manager manager = new Manager(settings);
     
    manager.Start();
     
    manager.LaunchNewBrowser();
    manager.ActiveBrowser.NavigateTo("http://aspspider.ws/kovyar/find_bycontent_test.htm");
    manager.ActiveBrowser.WaitUntilReady();
     
    Element element = manager.ActiveBrowser.Find.ByContent("mega link"); //null in both browsers, the desired way to find the element
     
    Element element1 = manager.ActiveBrowser.Find.ByContent("mega link".Replace(' ', (char)160)); //null in IE
    Element element2 = manager.ActiveBrowser.Find.ByContent("mega link".Replace(" ", "&nbsp;"));  //null in FF
     
    Assert.IsNotNull(element1); //here fails IE
    Assert.IsNotNull(element2); //here fails FF
     
    manager.Dispose();

    You can switch the browser type right in the first line of the code.
    The general trouble is that I cannot process this link in the same way in FF and IE.
    The second problem is that the types of spaces in the text can change (so the appearance of the text would be the same, but spaces would be different).

    So we need a way of finding elements on the page which will not depend on:
    1) browser type
    2) type of spaces used by developers.

    It would be really good if there is such a way, because our tests use Find.ByContent very often.

    Thanks,
    Yaroslav

  2. kovyar
    kovyar avatar
    42 posts
    Member since:
    Jul 2010

    Posted 19 Aug 2010 Link to this post

    Sorry, your parser has eaten the &nbsp; at the end of the thread name.
  3. Answer
    Konstantin Petkov
    Admin
    Konstantin Petkov avatar
    1911 posts

    Posted 19 Aug 2010 Link to this post

    Hi kovyar,

    I see your point and thank you for the details and page!

    Have you tried the framework LINQ and lambda expressions support? You can construct an expression to find the element which text content includes all the required words. Is this an acceptable solution for you?

    Sincerely yours,
    Konstantin Petkov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. kovyar
    kovyar avatar
    42 posts
    Member since:
    Jul 2010

    Posted 19 Aug 2010 Link to this post

    Hi Konstantin,

    and thanks for such a quick reply.
    Frankly speaking, I didn't know that such functionality is supported.
    Yes, this cool feature completely resolves this problem and one more we had.

    Thanks a lot,
    Yaroslav
  5. joe
    joe avatar
    5 posts
    Member since:
    Jan 2011

    Posted 27 Feb 2011 Link to this post

    is there a way to find by content without it being actual string value but a value within a variable? 
  6. Konstantin Petkov
    Admin
    Konstantin Petkov avatar
    1911 posts

    Posted 03 Mar 2011 Link to this post

    Hi joe,

    I don't see a problem doing that. Instead of hardcoded string you just need to pass the object value as argument. Do you experience any problems with that?

    All the best,
    Konstantin Petkov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top