【问题标题】:Get Text from SVG selenium java从 SVG selenium java 获取文本
【发布时间】:2023-04-05 01:31:01
【问题描述】:

我可以从 SVG 中的文本标签中获取文本。我正在附加 HTML

<svg width="553" height="200" style="overflow: hidden;" aria-label="A chart.">
     <defs id="defs">
     <rect x="0" y="0" width="553" height="200" stroke="none" stroke-width="0" fill="#ffffff">
     <g>
         <text text-anchor="start" x="77" y="22.85" font-family="Arial" font-size="11" font-weight="bold" stroke="none" stroke-width="0" fill="#000000">Clustering done on Mar 30, 2017 11:13 AM</text>
         <rect x="77" y="13.5" width="400" height="11" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
     </g>

我想得到的是Clustering done on Mar 30, 2017 11:13 AM

【问题讨论】:

标签: java html selenium testing svg


【解决方案1】:
driver.findElement(By.xpath("//*[local-name='svg']//*[local-name='g']//*[local-name='text']")).getText();

【讨论】:

    【解决方案2】:

    试试下面:

    driver.findElement(By.cssSelector("#divClusters svg > g‌ > text")).getText();
    

    【讨论】:

      【解决方案3】:

      这个定位器应该可以工作:

      • String text=driver.findElement(By.xpath("//[@id='divClusters']//*[name()='svg' and @aria-label='A chart.']//*[name()='g']")).getText(‌​); System.out.print(text);

      解释:

      • .//div - 删除 . 以搜索整个文档,而不仅仅是在当前选定的上下文元素中
      • 删除绝对的/div[1]/.. div 引用,因为它不可靠
      • 将svg元素引用为//*[name()='svg']
      • 为svg元素添加一些有意义的属性@aria-label='A chart.'
      • svg 中的元素应以相同的方式处理://*[name()="g"], //*[name()="defs"] - 感谢 Andersson 澄清这一点。

      【讨论】:

      • 不行,这样是找不到svg元素的
      • 我们应该使用*[name()='svg'] 而不是//svg 吗?
      • 是的。正是//*[name()='svg']
      • svg 中的元素应该以同样的方式处理://*[name()="g"], //*[name()="defs"]... 也修复//g
      猜你喜欢
      • 2020-11-18
      • 2017-12-01
      • 2019-10-10
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多