【问题标题】:How to get value from "text" tag which is inside SVG tag in selenium java如何从 selenium java 中的 SVG 标记内的“文本”标记中获取值
【发布时间】:2019-10-10 23:09:01
【问题描述】:

我正在尝试从图表上不可见的 SVG 元素内部获取文本值。但是,当我将鼠标悬停在它上面时,它会给出工具提示值

我可以将鼠标悬停在元素上,但无法获取值。

@FindBy(how=How.XPATH,using = "//*[name()='svg']//*[name()='g']//*[contains(@class,'')]//*[@visibility='hidden']")
    public static WebElement newgraph;

new Actions(driver).moveToElement(newgraph).clickAndHold().moveByOffset(0, 0).pause(1000).perform();
    /*    Actions builder = new Actions(driver);
        builder.moveToElement(newgraph).perform();*/
        String tooltip_msg = newgraph.getText();
        System.out.println(tooltip_msg);

以下是 HTML

<g class="highcharts-label highcharts-tooltip highcharts-color-undefined" 
style="cursor:default;pointer-events:none;white-space:nowrap;" 
transform="translate(212,-9999)" opacity="0" visibility="visible"><path 
fill="none" class="highcharts-label-box highcharts-tooltip-box" 
isShadow="true" stroke="#000000" stroke-opacity="0.049999999999999996" 
stroke-width="5" transform="translate(1, 1)"><text x="8" style="font- 
size:12px;color:#333333;fill:#333333;" y="20">

<tspan style="font-weight:bold; fill:#4fb3c3">Licensed: </tspan><tspan 
style="font-weight:bold" dx="0">560</tspan><tspan x="8" dy="15">May 24, 
2019</tspan></text>
</g>
</svg>

【问题讨论】:

  • 我要值2019年5月24日

标签: java selenium svg ui-automation


【解决方案1】:

你可以试试这个xpath:

//*[local-name() = 'svg']/*/*/*/*[name()='tspan'][3]

它正在定位你想要的节点。

只需找到使用.getText() 的网络元素即可提取日期。

如果您还有其他问题,请告诉我。

【讨论】:

  • 我在 XPath 上面试过,但得到“NoSuchElementException”
  • UI 中有多少 svg 元素?你可以去开发者工具,用//*[local-name() = 'svg']搜索,告诉我有多少条目。
  • 您是在询问图表的类型吗?
  • 不,我问的是网络元素?你怎么检查它呢? svg标签你可以在开发者工具中查看,只需按F12,然后选择元素选项卡。
  • 共有 3 个 SVG 元素存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多