【问题标题】:Find div element by multiple class names?通过多个类名查找 div 元素?
【发布时间】:2014-03-09 21:55:36
【问题描述】:

<div class="value test" /> 我想确定那个网络元素。它只定义了这两个类。 我不能执行以下操作,因为 className 不采用空格分隔值。有什么替代品?

@FindBy(className = "value test")
@CacheLookup
private WebElement test;

【问题讨论】:

    标签: java css selenium selenium-webdriver


    【解决方案1】:

    试试这个:

    test = driver.findElement(By.xpath("//div[@class='value test']"));
    

    【讨论】:

      【解决方案2】:

      我认为 barak manos 的回答没有完全解释它。

      假设我们有以下几个元素:

      1. <div class="value test"></div>
      2. <div class="value test "></div>
      3. <div class="first value test last"></div>
      4. <div class="test value"></div>

      XPath 如何匹配

      • 仅匹配 1(完全匹配),barak 的答案

        driver.findElement(By.xpath("//div[@class='value test']"));
        
      • 匹配1、2和3(匹配类包含value test,类顺序很重要)

        driver.findElement(By.xpath("//div[contains(@class, 'value test')]"));
        
      • 匹配 1、2、3 和 4(只要元素具有类 valuetest

        driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"));
        

      此外,在这种情况下,Css Selector 始终支持 XPath(快速、简洁、原生)。

      • 第 1 场比赛

        driver.findElement(By.cssSelector("div[class='value test']"));
        
      • 匹配 1、2 和 3

        driver.findElement(By.cssSelector("div[class*='value test']"));
        
      • 匹配 1、2、3 和 4

        driver.findElement(By.cssSelector("div.value.test"));
        

      【讨论】:

      • 感谢您的精彩解释,它在我的 selenium 实例中完美运行
      【解决方案3】:

      Class By.ByClassName

      By.ByClassName 类在By.java 中定义如下:

      /**
       * Find elements based on the value of the "class" attribute. If an element has multiple classes, then
       * this will match against each of them. For example, if the value is "one two onone", then the
       * class names "one" and "two" will match.
       *
       * @param className The value of the "class" attribute to search for.
       * @return A By which locates elements by the value of the "class" attribute.
       */
      public static By className(String className) {
        return new ByClassName(className);
      }
      

      这个用例

      因此,根据定义,您不能将多个类,即valuetest 作为参数传递给@FindBy(className = "...")。发送多个类将引发错误:

      invalid selector: Compound class names not permitted
      

      解决方案

      有多种方法可以解决这个用例,如下所示:

      • 如果元素仅通过classname 唯一标识value,您可以使用:

        @FindBy(className = "value")
        @CacheLookup
        private WebElement test;
        
      • 如果元素仅通过classname 唯一标识test,您可以使用:

        @FindBy(className = "test")
        @CacheLookup
        private WebElement test;
        
      • 如果classnamesvaluetest都是强制标识元素,您可以使用,如下所示:

        @FindBy(css  = ".value.test")
        @CacheLookup
        private WebElement test;
        
      • 您也可以使用,如下所示:

        @FindBy(xpath   = "//*[@class='value test']")
        @CacheLookup
        private WebElement test;
        

      tl;博士

      Invalid selector: Compound class names not permitted error using Selenium

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 2020-12-12
        • 1970-01-01
        • 2020-11-05
        • 2015-11-07
        • 2021-08-09
        • 1970-01-01
        相关资源
        最近更新 更多