如何使用Selenium从<text>标记获取文本

时间:2018-07-05 12:06:45

标签: selenium selenium-webdriver webdriver

我无法从下面提到的标记上的Web元素获取文本:

<text dy="-.1em" class="c3-gauge-value" transform="" style="opacity: 1; text-anchor: middle; pointer-events: none; font-size: 9px;">95</text>

需要获取值“ 95”,该值将在请勿图表内显示。

我正面临问题:

no such element: Unable to locate element: {"method":"css
selector","selector":"#chart > svg > g:nth-child(2) > g.c3-chart >
g.c3-chart-arcs > g > text"}

任何人都可以提供任何解决方案。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用xPath查找元素:

Name    Subject     Score
Harry   Math         4
Harry   Science      5
Harry   Social       3
Harry   French       5
Harry   Spanish      4
Steve   Math         5
Steve   Science      3
Steve   Social       5
Steve   French       4
Steve   Spanish      4

此外,可能有必要添加//text[@dy = '-.1em' and @class='c3-gauge-value'] 方法,直到元素将被附加到DOM。另外,请确保您的元素不在wait中。

我不知道您使用哪种语言,这就是为什么我无法为您的案例提供具体的示例代码。但是您可以在此站点上搜索,有很多非常好的示例。

答案 1 :(得分:0)

您可以通过以下方式访问其文本

driver.findElement(By.cssSelector(".c3-gauge-value")).getText();