如何在自动化期间单击Highchart元素

时间:2018-07-10 14:50:43

标签: java selenium selenium-webdriver highcharts selenium-chromedriver

有什么方法可以获取SmartGWT hightchart图中元素的定位符(sclocator或xpath等)吗?我需要自动执行对图形系列图例(显示在图形右侧的系列)的点击操作。

这些是我试图为该图的元素标识有效位置的东西:

  • 使用相对Xpath帮助器插件。单击highchart图的任何元素后,我得到以下消息:“未找到唯一标识符”。
  • 使用为“ smartgwt”设置了带有“ user-extensions.js”和“ user-extensions-ide.js”硒库的Selenium IDE。在尝试标识这些系列或图形中任何其他元素的定位器之后,我收到消息:“ LOCATOR_DETECTION_FAILED”。
  • 使用Selenium Page Object Generator插件。生成Java文件后,Java文件中未生成与图像有关的信息。

任何帮助将不胜感激。

谢谢!

Graph Highchart

<g class="highcharts-legend" zIndex="7" transform="translate(-32,110)">
  <g zIndex="1" clip-path="url(#highcharts-2)">
    <g transform="translate(0,1)">
      <g class="highcharts-legend-item" zIndex="1" transform="translate(8,3)">
        <path fill="none" d="M 0 11 L 16 11" stroke-dasharray="none" stroke="#C00000" stroke-width="2"></path>
        <path fill="#C00000" d="M 8 9 C 10.664 9 10.664 13 8 13 C 5.336 13 5.336 9 8 9 Z"></path>
        <text x="21" y="15" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;cursor:pointer;color:#274b6d;fill:#274b6d;" text-anchor="start" zIndex="2">
          <tspan x="21">Worst Inventory On Site</tspan>
        </text>
      </g>
      <g class="highcharts-legend-item" zIndex="1" transform="translate(8,21)">
        <path fill="none" d="M 0 11 L 16 11" stroke-dasharray="none" stroke="#3A5723" stroke-width="2"></path>
        <path fill="#3A5723" d="M 8 9 C 10.664 9 10.664 13 8 13 C 5.336 13 5.336 9 8 9 Z"></path>
        <text x="21" y="15" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;cursor:pointer;color:#274b6d;fill:#274b6d;" text-anchor="start" zIndex="2">
          <tspan x="21">Total Ins</tspan>
        </text>
      </g>
      <g class="highcharts-legend-item" zIndex="1" transform="translate(8,39)">
        <path fill="none" d="M 0 11 L 16 11" stroke-dasharray="none" stroke="#CCC" stroke-width="2"></path>
        <path fill="#CCC" d="M 8 9 C 10.664 9 10.664 13 8 13 C 5.336 13 5.336 9 8 9 Z"></path>
        <text x="21" y="15" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;cursor:pointer;color:#CCC;fill:#CCC;" text-anchor="start" zIndex="2">
        <tspan x="21">Total Outs</tspan>
       </text>
      </g>
      <g class="highcharts-legend-item" zIndex="1" transform="translate(8,57)">
        <path fill="none" d="M 0 11 L 16 11" stroke-dasharray="2,2" stroke="#CCC" stroke-width="2"></path>
        <path fill="#CCC" d="M 8 9 C 10.664 9 10.664 13 8 13 C 5.336 13 5.336 9 8 9 Z"></path>
        <text x="21" y="15" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;cursor:pointer;color:#CCC;fill:#CCC;" text-anchor="start" zIndex="2">
          <tspan x="21">Inventory On Site</tspan>
       </text>
      </g>
      <g class="highcharts-legend-item" zIndex="1" transform="translate(8,75)">
        <path fill="none" d="M 0 11 L 16 11" stroke-dasharray="6,2" stroke="#CCC" stroke-width="2"></path>
        <path fill="#CCC" d="M 8 9 C 10.664 9 10.664 13 8 13 C 5.336 13 5.336 9 8 9 Z"></path>
        <text x="21" y="15" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;cursor:pointer;color:#CCC;fill:#CCC;" text-anchor="start" zIndex="2">
        <tspan x="21">Inventory On Transit</tspan>
       </text>
      </g>
    </g>
  </g>
</g>

2 个答案:

答案 0 :(得分:2)

感谢@ dangi13的帮助。但是我找到了使用不同的xpath结构的解决方案:

sharedData.appInstance.findElement(By.xpath("//[name()='g' and @class='highcharts-legend']//[name()='text']//*[name()='tspan' and text()='Worst Inventory on Site']")).click();

答案 1 :(得分:1)

您可以使用以下代码单击HighChart Elements:

/**
     * @param graphName Use one of below values.
     * Worst Inventory On Site
     * Total Ins
     * Total Outs
     * Inventory On Site
     * Inventory On Transit
     */
    public void clickOnGraph(String graphName) {
         WebElement graphElement = driver.findElement(By.xpath("//g[@class='highcharts-legend-item']//tspan[text()='" + graphName + "']"));
         graphElement.click();
    }

有关进一步的HighChart元素操作。您可以参考:

https://github.com/Ardesco/Powder-Monkey/blob/master/src/main/java/com/lazerycode/selenium/graphs/HighCharts.java

对于与HighCharts相关的Selenium操作非常有用。

如果常规点击不起作用,则可以尝试使用JavaScriptExecutor或Actions类进行点击。

请告诉我是否对您有帮助:)