C# Selenium - 获取元素 html 而不是文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16862100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:58:43  来源:igfitidea点击:

Selenium - Get elements html rather Text Value

c#html-parsingselenium-webdriver

提问by LoneXcoder

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

通过该代码,我从 html 文档中提取了所有所需的文本

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

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 HTMLand not the extracted text ?

正如您在我的代码中看到的,我能够将文本部分的值作为文本值获取,这很好,但是如果我想将容器的值作为HTML而不是提取的文本呢?

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

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

所以容器在一个页面中大约有 10 次,我需要提取它的 innerHtml 。

any ideas ? (using Selenium)

有任何想法吗 ?(使用硒)

采纳答案by Yi Zeng

Find the element first, then use IJavaScriptExecutorto get the inner HTML.

首先找到元素,然后使用IJavaScriptExecutor获取内部 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);
}

回答by Oli Wennell

This seemed to work for me, and is less code:

这似乎对我有用,而且代码更少:

var element = driver.FindElement(By.ClassName("sa_wr"));
var innerHtml = element.GetAttribute("innerHTML");