0

I am trying to inspect a text on a page and when I inspect it, I see this:

 < div class="welcome-message">
   < h1 data-bind="text: $.t('wfo.Common:wfo.Welcome'), attr: { title:    $.t('wfo.Common:wfo.Welcome') }" title="Welcome">Welcome</h1>

In my code , I am using the Xpath to find the element, then trying to verify this

Assert.IsTrue(AdminPage.IsAt, "Not In Admin Page");

by implementing below code:

  public static bool IsAt { 
       get{
          var h3 = Driver.instance.FindElements(By.XPath("//div[@class='welcome-message']/h1[@title='Welcome']"));
            if (h3.Count >0)
                return true;
            return false;
        }
     }

But when I run the case, I am getting the failure: The "Not In Admin Page" Error message is seen.

What I am trying to do is to return a list of IwebElements per the searching criteria, and a true response should be returned when the element is found, Until now, I am assuming that I want to match the "True" criteria. I am new in Selenium, your help on this will be highly appreciated, thanks a lot.

Murthi
  • 5,299
  • 1
  • 10
  • 15
J.FromHeaven
  • 343
  • 3
  • 9

4 Answers4

0

A few things I would look into:

  1. Try using the contains option in h1: //div[@class="'welcome-message']/h1[contains(@title='Welcome')

  2. Xpath is generally the least reliable out of the find element methods, using ID, classname, or even CSS selector are much better for this if the option is available. Obviously sometimes this is not possible. By.ClassName('welcome-message');

  3. Mix all and filter down on the children. IWebElement parent = Driver.FindElement(By.CssSelector("div[class='welcome-message'])); IWebElement child = parent.FindElement(By.XPath(".//h1[@title='Welcome]"));

  4. Allow time for the page to load. Selenium has methods to wait for elements to load, I won't go into detail but an example is here.
Jonnyboy
  • 136
  • 1
  • 8
0

try this :

public static bool IsAt { 
       get{
          var h3 = Driver.instance.FindElements(By.CSSSELECTOR("div.welcome-message>h1[title='Welcome']"));
            if (h3.Count >0)
                return true;
            return false;
        }
     }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thanks a lot @cruisepandey. I used this and went through it: public static bool IsAt { get { var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]")); if (h3!=null) return true; return false; } }' – J.FromHeaven Jun 25 '18 at 05:04
0

I am not sure.. but I think this error occured because your method "IsAt" is static, delete this modifier and try again

0

Thanks a lot @cruisepandey. I used this and went through it:

  public static bool IsAt
    { get

        {

     var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));

               if (h3!=null)
            return true;

            return false;
        }  

And when I replace your

"if" condition (h3.Count >0) as (h3 !=null);

I also get the expected result.

"FindElements" should return a list of items matching the search criteria, and I am not just sure why the if (h3.Count >0) is not working in this case?

J.FromHeaven
  • 343
  • 3
  • 9