如何从子文本节点中提取文本 - Selenium

时间:2018-04-26 10:21:29

标签: c# selenium-webdriver xpath

从下面的代码我想提取:"我给你€0.00。"。

使用以下xpath我可以从Developer工具中定位它:

  

// * [@ id中=" WINDOWID"] /文本()[2]

但是使用Selenium我只能使用driver.findElement(By.Xpath(""))

查找此元素

问题是它说xpath表达式的结果不是元素,而是[object Text]

如果我只定位祖先div(windowID),则接收整个文本。我只希望将提到的部分保存为字符串。

<div id="windowID" class="windowClass">"Your are awesome."<br>"I give you €0.00."<br>"But you should give me €0.00."<br>"Would you like to do that?"</div>

2 个答案:

答案 0 :(得分:0)

在这里,我获取所有文本并按<br>拆分文本,我们可以通过分隔打印文本。

 String mytext=driver.findElement(By.xpath("//div[@id='windowID'].getAttribute("innerHTML");
    String[] separatestring= mytext.split(Pattern.quote("<br>"));
    String firstone= separatestring[0];
    String secondtone= separatestring[1];
    System.out.println(firstone);
    System.out.println(secondtone);

我是用java做的,在这里你可以把它转换成C#。希望它会对你有所帮助。

答案 1 :(得分:0)

要提取文字我给你€0.00。你可以使用以下代码行:

IWebElement elem = driver.FindElement(By.XPath("//div[@class='windowClass' and @id='windowID']"));
string text = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].childNodes[3].textContent;", elem);
相关问题