SimpleBrowserDotNet/SimpleBrowser

Need some code samples for JS counterpart: getElementsByClassName

johngit2020 opened this issue · 4 comments

Hello:
I have a new project which needs to extract some dynamic text from a web site, but I don’t need image or other things. I found SimpleBrowser seems to be a good tools to do my job, as my program language is C# on Windows 10.
However, I can’t find enough examples and/or tutorial to continue my project. The biggest issue is: I don’t know how to do the task as in JavaScript: getElementsByClassName.
I created the following simple HTML on my local PC: http://localhost/TestClass.html
With the following HTML contents:

TestClassHtml

If I open Chrome browser and visit: http://localhost/TestClass.html
And in developer tools, I can use this JavaScript command to get some HTML elements:
var titles = document.getElementsByClassName('title');
However, when I use SimpleBrowser, I can navigate to http://localhost/TestClass.html
And see the CurrentHtml, it works well.
But how I can use SimpleBrowser to get variable as I did with JavaScript:
var titles = document.getElementsByClassName('title')
Please advice.
Thanks,

@johngit2020

There are some code samples at:
https://github.com/SimpleBrowserDotNet/SimpleBrowser/wiki/Tutorial

To your specific request, you can find elements by class name using the Browser.Select() method. I haven't used it much, but I think it's something like this:

var titles = b.Select("span[@class='title']");

or the Browser.Find() method, which I've used most:

var titles = browser.Find("span", FindBy.Class, "title");

That should do what you need.

Kevin

Thanks for your reply. Your code works.
But I have another minor issue: how I can display HTML code in here, you can see I upload the picture, as I can't figure out how to show HTML code here, if I use the same code as displayed in the picture, you will see the following:

Blog Name

Match Title
1.55

Please advice, as I have other similar questions to ask, and I don't want to upload a picture each time.
Thanks,

Hello:
Thanks, your code works.
But I want to know how to display HTML code here, as you can see I upload a picture in order to show the HTML code, since I can't figure out how to display HTML code here.
Please advice!