代码之家  ›  专栏  ›  技术社区  ›  johnsonambrose

验证svg下元素的填充颜色

  •  0
  • johnsonambrose  · 技术社区  · 7 年前

    我试图验证svg下页面上多个元素的填充颜色。这7个元素的相对xpath都是相同的。

    //*[@id=“svg_health-state-success”]/path

    HTML格式

    <div class="v-object-modifiers hide" xpath="1"> </div>
    <svg role="img" class="c-icon c-icon-health-state-success" xpath="1"><use 
    xlink:href="#svg_health-state-success"></use></svg>
    <use xlink:href="#svg_health-state-success"></use>
    <svg id="svg_health-state-success" viewBox="0 0 62.5 62.5" width="100%" 
    height="100%"><path style="fill:#00a651" d="M0 0v62.5h62.5V0H0zm25.7 
    48.2L9.5 32l8.6-8.6 7.8 8.4 20.9-17.4 6.2 6.4-27.3 27.4z"></path></svg>
    <path style="fill:#00a651" d="M0 0v62.5h62.5V0H0zm25.7 48.2L9.5 32l8.6-8.6 
    7.8 8.4 20.9-17.4 6.2 6.4-27.3 27.4z"></path>
    </svg>
    </use>
    </svg>
    </div>
    

    我试着在我的剧本中使用下面的,它不起作用

    String XPATHONE = "//*[name()='svg']//*[name()='path' and @fill='#00a651' and position()=2]";
    WebElement svgObj = driver.findElement(By.xpath(XPATHONE));
    Actions actionBuilder = new Actions(driver);
    actionBuilder.click(svgObj).build().perform();
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Andersson    7 年前

    fill 不是 属性 属于 path 元素,但是 CSS样式属性 ,所以您不能使用 @fill 语法。尝试下面的xpath:

    //*[name()='svg']//*[name()='path' and contains(@style, 'fill="#00a651"')]
    

    我也不确定 position()=2 谓词,因为它看起来不像 路径 节点有任何同级(如果实际有同级,请正确更新HTML源示例)