【问题标题】:How to extract text from child Text Nodes - Selenium如何从子文本节点中提取文本 - Selenium
【发布时间】:2018-04-26 10:21:28
【问题描述】:

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

使用以下 xpath,我可以从开发人员工具中定位它:

//*[@id="windowID"]/text()[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>

【问题讨论】:

    标签: c# selenium selenium-webdriver xpath webdriver


    【解决方案1】:

    要提取文本I give you €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);
    

    【讨论】:

      【解决方案2】:

      这里我得到了所有的文字,用&lt;br&gt;分割文字,我们可以通过分割打印文字。

       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#。希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-05
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 2017-01-07
        • 2018-11-15
        • 2020-11-01
        相关资源
        最近更新 更多