代码之家  ›  专栏  ›  技术社区  ›  chhenning

确定html节点的样式

  •  0
  • chhenning  · 技术社区  · 6 年前

    我有许多用MS Word生成的html文件。html代码主要是包含文本节点、段落、跨度等的表。这些文件不包含javascript或显示任何其他动态行为。但是他们有CSS样式。

    如果htmlagilitypack无法解析CSS样式,那么最好的工具是什么?我应该改用硒吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   chhenning    6 年前

    Selenium通过Javascript获得了答案。

    ChromeOptions Options = new ChromeOptions();
    Options.AddArgument("--headless");
    IWebDriver Driver = new ChromeDriver(Options);
    Driver.Navigate().GoToUrl(@"file://Some_File.html");
    
    // The correct style
    var properties = ((IJavaScriptExecutor)Driver).ExecuteScript("return window.getComputedStyle(arguments[0],null).cssText", e);
    
    Driver.Close();
    Driver.Quit();