Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SeleniumWebDriver C获取DOCTYPE#_C#_Selenium_Webdriver_Doctype_W3c Validation - Fatal编程技术网

C# 使用SeleniumWebDriver C获取DOCTYPE#

C# 使用SeleniumWebDriver C获取DOCTYPE#,c#,selenium,webdriver,doctype,w3c-validation,C#,Selenium,Webdriver,Doctype,W3c Validation,我使用SeleniumWebDriver实现UI自动化。下面是我的示例代码 IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver(); string url ="http://stackoverflow.com"; driver.Navigate().GoToUrl(url); string pagesource = driver.PageSource; pagesource变量没有docty

我使用SeleniumWebDriver实现UI自动化。下面是我的示例代码

IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver();
    string url ="http://stackoverflow.com";
    driver.Navigate().GoToUrl(url);
    string pagesource = driver.PageSource;
pagesource变量没有doctype。我需要知道你的文档类型。有没有办法通过selenium获取html源代码的DOCTYPE


显示无法通过selenium获取html源代码的Doctype,相反,您可以从.net执行HTTP请求并获取Doctype。我不想为获取DOCTYPE执行单独的HTTP请求

使用FirefoxDriver而不是InternetExplorerDriver将获得DOCTYPE。不幸的是,这并不能解决您的问题-您使用driver.PageSource获得的源代码已经由浏览器进行了预处理,因此尝试验证该代码不会给出可靠的结果

不幸的是,没有简单的解决办法

如果您的页面没有密码保护,您可以使用“通过uri验证”方法


否则,您需要获取页面源。我知道两种方法(我在我的项目中实现了这两种方法)。一种是使用代理。如果您使用的是C#,请查看。另一种方法是使用javascript和XMLHttpRequest发出另一个请求。您可以(在页面上搜索XMLHttpRequest)。

对于W3C验证,如果通过selenium webdriver实现自动化,基本上我们有3个问题

  • 从驱动程序获取正确的页源。页源不可靠
  • 正在获取HTML源的doctype
  • 处理通过ajax调用呈现的控件。既然我们无法在页面源代码中访问这些控件,我们如何获得页面的确切“生成源代码”
  • 通过SeleniumWeb驱动程序执行javascript可以完成上述所有工作

    在名为“htmlsource.txt”的文本文件中,存储下面的代码段



    但是你仍然面临着同样的问题——html代码是从DOM中获取的,由浏览器进行预处理,因此不可靠。但是对于通过Ajax呈现的控件,有没有办法进行W3C验证?FiddlerCore不会有帮助,因为控件是作为数据呈现的,javascript会将所有数据转换为HTML控件。
    function outerHTML(node){
        // if IE, Chrome take the internal method otherwise build one as lower versions of firefox
            //does not support element.outerHTML property
      return node.outerHTML || (
          function(n){
              var div = document.createElement('div'), h;
              div.appendChild( n.cloneNode(true) );
              h = div.innerHTML;
              div = null;
              return h;
          })(node);
      }
    
    
     var outerhtml = outerHTML(document.getElementsByTagName('html')[0]);
    var node = document.doctype;
    var doctypestring="";
    if(node)
    {
         // IE8 and below does not have document.doctype and you will get null if you access it.
    
     doctypestring = "<!DOCTYPE "
             + node.name
             + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
             + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
             + (node.systemId ? ' "' + node.systemId + '"' : '')
             + '>';
             }
             else
    
             {
    
                 // for IE8 and below you can access doctype like this
    
             doctypestring = document.all[0].text;
             }
    return doctypestring +outerhtml ;
    
     IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
                string jsToexecute =File.ReadAlltext("htmlsource.txt");
                string completeHTMLGeneratedSourceWithDoctype = (string)js.ExecuteScript(jsToexecute);