C# 使用SeleniumWebDriver C获取DOCTYPE#
我使用SeleniumWebDriver实现UI自动化。下面是我的示例代码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
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代码是从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);