I had issues with this bug with my automation software. Not sure if you can relate to this feeling but if things are not perfect it will come back and bite you even harder. So I finally told myself I need to deal with this somehow. So I found this thread talking about this issue.
https://github.com/cefsharp/CefSharp/issues/4621
Reproducing the error
I know Alex is very busy so in order to help him out I found the perfect place to reproduce the error:
Open this url in CefBrowser
https://chat.openai.com/
Then click Login.
Here you get 2 redirects.
On the login page you try to retrieve HTML.
I use this HORRIBLE code to do it. (Please forgive me Alex)
- Code: Select all
string script = @"
var nodelist = document.querySelectorAll( `" + arg2 + @"` );
var elements = [];
for(var i=0; i <nodelist.length; i++)
{
if(nodelist[i].outerHTML.includes(`" + arg3 + @"`))
{
var getall = `" + getall.ToString() + @"`;
elements.push(nodelist[i].outerHTML);
if(getall == 'False')
{
break;
}
}
}
elements;
";
if (CheckItemLocalOrGlobal(arg4))
{
// Use await to asynchronously wait for the script evaluation to complete
var response = await frame.EvaluateScriptAsync(script);
if (response.Success && response.Result != null)
{
// Cast the result to a List<string> as it represents a list of HTML strings
var data = ((List<object>)response.Result).Cast<string>().ToList();
// Marshal the update back to the UI thread if necessary
form1.Invoke(new MethodInvoker(() =>
{
if (!getall)
{
SetTextLocalOrGlobal(arg4, data.FirstOrDefault());
}
else
{
foreach (string element in data)
{
AddTextLocalOrGlobal(arg4, element + "\r\n");
}
}
}));
}
}
}
catch (Exception ex)
{
CheckLDM("GetKnownHtml: Exception'" + ex.Message + "' ");
}
How to get around the problem?
Well the solution is even more embarrassing
but it should give some guidance to further narrow things down.
So I basically setup a RequestHandler:
- Code: Select all
int tabidx = int.Parse(Thread.CurrentThread.Name);
ChromiumWebBrowser chrome = tabControl1.TabPages[tabidx].Controls[0] as ChromiumWebBrowser;
if (chrome.RequestHandler == null)
{
chrome.RequestHandler = customRequestHandler;
}
var handler = (CustomRequestHandler)chrome.RequestHandler;
SetTextLocalOrGlobal(arg2, handler.RedirectUrl);
The Request Handler shows all the stream of urls that the browser wants to load and we can take control of how the browser reacts.
- Code: Select all
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
public string CurrentUrl { get; set; }
public string RedirectUrl { get; set; }
public bool PreventRedirect { get; set; }
public bool StepRedirect { get; set; }
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
if (StepRedirect && !request.Url.Equals(RedirectUrl, StringComparison.OrdinalIgnoreCase))
{
RedirectUrl = request.Url;
chromiumWebBrowser.LoadUrl("about:blank");
//chromiumWebBrowser.LoadUrl("about:blank");
//Thread.Sleep(2000);
//return true; // Cancel the navigation
}
if (CurrentUrl == null)
{
CurrentUrl = request.Url;
}
if (PreventRedirect && frame.IsMain && !request.Url.Equals(CurrentUrl, StringComparison.OrdinalIgnoreCase))
{
return true; // Cancel the navigation
}
return base.OnBeforeBrowse(chromiumWebBrowser, browser, frame, request, userGesture, isRedirect);
}
}
So with the section where I use StepRedirect I want to take control of the browser when there is any difference between the old url and the new url.
Then I simply do a load url "about:blank" to just tell the browser to load this instead. I'm not sure exactly what happens with this hack but it works. (It's not funny! )
You can see that I store the new url inside RedirectUrl so that I can retrieve it with
- Code: Select all
handler.RedirectUrl
Now I can carefully load every url one at a time with browser.load("https://ww")
When I do that I can retrieve the HTML and I don't get this error.
I'm sure there is a way to refine this hack to make it automagically but that is up to Alex the real wizard. I'm just an apprentice level 3. Alex is Level 999.
I hope this help.