硒-获取元素html而不是文本值 [英] Selenium - Get elements html rather Text Value

查看:144
本文介绍了硒-获取元素html而不是文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过该代码,我已经从html文档中提取了所有想要的文本

Via that code i have extracted all desired text out of a html document

private void RunThroughSearch(string url)
{
    private IWebDriver driver;
    driver = new FirefoxDriver();
    INavigation nav = driver.Navigate();
    nav.GoToUrl(url);

    var div = driver.FindElement(By.Id("results"));
    var element = driver.FindElements(By.ClassName("sa_wr"));
}

尽管我需要优化提取文档的结果

though as i need to refine results of extracted document

Container
    HEADER -> Title of a given block
    Url -> Link to the relevant block
    text -> body of a given block
/Container

如您在我的代码中所见,我能够获取文本部分的值 作为文本值,这很好,但是如果我想拥有 容器的值为 HTML ,而不是提取的文本?

as u can see in my code i am able to get the value of the text part as a text value , that was fine, but what if i want to have the value of the container as HTML and not the extracted text ?

<div class="container">
    <div class="Header"> Title...</div>
    <div class="Url"> www.example.co.il</div>
    <div class="ResConent"> bla.. </div>
</div>

因此该容器在一页中大约是10次 我需要提取它的innerHtml.

so the container is about 10 times in a page i need to extract it's innerHtml .

有什么想法吗? (使用硒)

any ideas ? (using Selenium)

推荐答案

首先查找元素,然后使用

Find the element first, then use IJavaScriptExecutor to get the inner HTML.

var element = driver.FindElements(By.ClassName("sa_wr"));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
if (js != null) {
    string innerHtml = (string)js.ExecuteScript("return arguments[0].innerHTML;", element);
}

这篇关于硒-获取元素html而不是文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆