Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
.net IHTMLSelectionObject.createRange()引发UnauthorizedAccessException_.net_Internet Explorer_Dom_Webbrowser Control_Bho - Fatal编程技术网

.net IHTMLSelectionObject.createRange()引发UnauthorizedAccessException

.net IHTMLSelectionObject.createRange()引发UnauthorizedAccessException,.net,internet-explorer,dom,webbrowser-control,bho,.net,Internet Explorer,Dom,Webbrowser Control,Bho,我编写了以下代码,用于从当前网页检索所选文本: IHTMLDocument2 mainDoc = ... for ( int i = 0; i < mainDoc.frames.length; i++ ) { object refIndex = i; var frame = (IHTMLWindow2)mainDoc.frames.item( ref refIndex ); IHTMLDocument2 frameDoc; try { fra

我编写了以下代码,用于从当前网页检索所选文本:

IHTMLDocument2 mainDoc = ... for ( int i = 0; i < mainDoc.frames.length; i++ ) { object refIndex = i; var frame = (IHTMLWindow2)mainDoc.frames.item( ref refIndex ); IHTMLDocument2 frameDoc; try { frameDoc = frame.document; } catch ( UnauthorizedAccessException ex ) { // Source: http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html var sp = (IServiceProvider)frame; // Use IServiceProvider.QueryService to get IWebBrowser2 object. object brws = null; sp.QueryService( ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws ); // Get the document from IWebBrowser2. IWebBrowser2 browser = (IWebBrowser2)brws; frameDoc = (IHTMLDocument2)browser.Document; } var range = frameDoc.selection.createRange() as IHTMLTxtRange; if ( !string.IsNullOrEmpty(range.text) ) return range.text; } return string.Empty; IHTMLDocument2主文档=。。。 对于(int i=0;i System.UnauthorizedAccessException was unhandled Message="Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" Source="Microsoft.mshtml" StackTrace: at mshtml.IHTMLSelectionObject.createRange() System.UnauthorizedAccessException未处理 Message=“访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))” Source=“Microsoft.mshtml” 堆栈跟踪: 在mshtml.IHTMLSelectionObject.createRange()中
如何避免此错误?

这是预期的行为,默认情况下已禁用,并且您正在跨帧执行脚本,而不考虑其域。

已经有一段时间了。但是您解决过这个问题吗?
frameDoc.selection.createRange()
不一定返回IHTMLTxtRange-可能先查询
selection.type
属性?看见