Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我使用Xpath在Selenium C中搜索元素时,我的测试总是失败#_C#_Selenium_Xpath_Selenium Chromedriver - Fatal编程技术网

C# 当我使用Xpath在Selenium C中搜索元素时,我的测试总是失败#

C# 当我使用Xpath在Selenium C中搜索元素时,我的测试总是失败#,c#,selenium,xpath,selenium-chromedriver,C#,Selenium,Xpath,Selenium Chromedriver,我试图检查页面上的文本,当我检查它时,我看到: < div class="welcome-message"> < h1 data-bind="text: $.t('wfo.Common:wfo.Welcome'), attr: { title: $.t('wfo.Common:wfo.Welcome') }" title="Welcome">Welcome</h1> public static bool IsAt { get

我试图检查页面上的文本,当我检查它时,我看到:

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

        {

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

               if (h3!=null)
            return true;

            return false;
        }  
通过实现以下代码:

  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;
        }
     }
  public static bool IsAt
    { get

        {

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

               if (h3!=null)
            return true;

            return false;
        }  
但是当我运行这个案例时,我得到了失败:看到了“notin Admin Page”错误消息

  public static bool IsAt
    { get

        {

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

               if (h3!=null)
            return true;

            return false;
        }  

我试图做的是根据搜索条件返回一个IwebElements列表,并且在找到元素时应该返回一个true响应,直到现在,我假设我想要匹配“true”条件。我是硒行业的新手,非常感谢您在这方面的帮助。

我将了解以下几点:

  public static bool IsAt
    { get

        {

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

               if (h3!=null)
            return true;

            return false;
        }  
  • 尝试使用h1中的contains选项:
    
    //div[@class=“'welcome-message']/h1[包含(@title='welcome')
    

  •   public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
  • Xpath通常是find元素中最不可靠的方法,如果有可用的选项,那么使用ID、classname甚至CSS选择器会更好。
    
    By.ClassName('welcome-message');
    

  •   public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
  • 把所有的东西混合在一起,过滤掉孩子们。
    
    IWebElement parent=Driver.FindElement(By.CssSelector(“div[class='welcome-message']);
    IWebElement child=parent.FindElement(By.XPath(“.//h1[@title='Welcome]”);
    

  •   public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
  • 为页面加载留出时间。Selenium有等待元素加载的方法,我不详细介绍,但举了一个例子
  • 试试这个:

    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;
            }
         }
    
      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    

    我不确定..但我认为发生此错误是因为您的方法“IsAt”是静态的,请删除此修饰符并重试

    非常感谢@cruisepandey。我使用了此方法并检查了它:

      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
    当我替换你的

      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
    “如果”条件(h3.Count>0)为(h3!=null)

      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    
    我也得到了预期的结果

      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }  
    

    “FindElements”应该返回一个与搜索条件匹配的项目列表,我不知道为什么会出现if(h3.Count>0)在这种情况下不起作用吗?

    你有HTML吗?发送建议请在问题后面留下评论。非常感谢@cruisepandey。我使用了它并进行了检查:public static bool IsAt{get{var h3=Driver.instance.FindElements(By.XPath(“/*[contains(text(),'Welcome')]”);if(h3!=null)返回true;返回false;}}
      public static bool IsAt
        { get
    
            {
    
         var h3 = Driver.instance.FindElements(By.XPath("//*[contains(text(), 'Welcome')]"));
    
                   if (h3!=null)
                return true;
    
                return false;
            }