Hello,
I am using the CefSharp Chromium browser control.
I now enter the url: "https://www.test.com/". Now I try to get all class names which exist on this URL using the code below but I get the error for this line like this:
MessageBox.Show($"{((dynamic)result.Result).Count}"); //**Cannot perform runtime binding on a null reference**
I wonder how I can get all class names which exist on this URL, - in a List correctly with the below code?
This code installs the cefsharp browser from Nuget:
Install-Package CefSharp.WinForms -Version 91.1.211
Thank you!
using CefSharp;
using CefSharp.WinForms;
public ChromiumWebBrowser browser;
public void InitBrowser()
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("https://uniswapv3.flipsidecrypto.com");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
public Form1()
{
InitializeComponent();
InitBrowser();
}
async void getAllClasses()
{
var script = @"(function() { return document.querySelectorAll('[class]'); })();";
var result = await browser.EvaluateScriptAsync(script);
//How to get all classes in a List?
List<String> allClassLIST = new List<String>();
MessageBox.Show($"{((dynamic)result.Result).Count}"); //Cannot perform runtime binding on a null reference
}
@Andreas ss
Did you get Result after removing await? When I tested it, it was still null.
This is still the cause of the problem. When I changed the class in js to div, the problem no longer appeared.
You can also add a if statement, if it is empty, these statements are not executed:
Thank you, yes, it seems to have dissapeared when changing to div. I added the if statement there also. It was a good idéa!
However, it seems that I still get this error below. I am not sure exactly how to iterate all DIV as this example is. I thought the obj would hold a list of all DIV somehow but something might be missing?
The variable obj gets the below error:
Cannot implicitly convert type 'CefSharp.JavascriptResponse' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?
@Andreas ss
When we use await, the result of this code is the object of JavascriptResponse.
At this point, we use
dynamic obj = result.Result;
to get JavascriptResponse.Result.When we do not use await, it is TaskJavascriptResponse.
What result.Result gets is Task.Result, the JavascriptResponse object, so we need to call its Result, like this:
Thank you TimonYang, now I understand. There was a difference there. This was good to understand.
I got a bit confused there but this makes sense now. I managed it to work like this:
Thank you for your help!
Sign in to comment