How can I make elements visible in C# Webbrowser? - c#

HTML code (example):
<div class="clsA"><div class="clsA1">TEXT</div></div> <div class="clsB"></div>
I want make DIV which have class-attribute is clsA and their child-elements visible.
(both clsA and clsA1 appear).
I try
foreach (HtmlElement pageElement in webBrowser1.Document.All)
{
if (pageElement.GetAttribute("className").ToLower() != "clsa")
pageElement.Style = "display:none";
}
but everything disappear.
Thanks

The uppercase A in "clsA" will never match a to lower string, so you will set every item on the page to display:none.

Related

blazor StateHasChanged not working in a loop

I have a Blazor Server app. I dynamically add a custom control (which I created in a Razor Class Library) to my page, like this:
<div class="container ">
<div class="row ">
<div class="col-12">
<label>#p_section.question</label>
</div>
</div>
#foreach (object new_control_model in p_section.list_of_control_models)
{
DisaggregateControlModel new_disag_model = (DisaggregateControlModel)new_control_model;
<DisaggregateControl #ref="myComponents[new_disag_model.id]" model="new_disag_model">
</DisaggregateControl>
}
</div>
This add the control to my dictionary, which I can access.
#code {
private Dictionary<string, object> myComponents = new Dictionary<string, object>();
}
In the custom control, I have a method that sets a bool, which allows me to display or hide a Div. In the web component that has this code, I want to iterate over all the objects in myComponents and either turn on or off the div display. I do that like this:
foreach (string id in some_list_of_ids){
//find the object in myComponents list
object found_obj = myComponents.FirstOrDefault(x => x.Key == id).Value;
//cast to my custom control
DisaggregateControl myControl = (DisaggregateControl)found_obj;
// based on property, determine if I should show the div or not
if(myControl.some_vale >0){
//show
myControl.showDiv(true);
}else{
myControl.showDiv(false);
}
}
//updated all the controls, so update the page
StateHasChanged();
If I debug and walk through, I can see that the code works. The correct divs are shown/hidden. Until the code reaches StateHasChanged(), and then all the divs are hidden. If I remove StateHasChanged then the code also does not work (the divs are not shown, when they should be).
I am not sure what the issue is or how to best handle this?
Turns out that it was an issue with my variable. In the custom library, I showed/hid the div by setting a bool value. But, I inadvertently made the bool static. Once I changed that, it worked fine. I assume because the variable was static, once I changed it for one control, it got changed for all controls.

grab value of a tag text using geckoWebBrowser

Check the code bellow. I am working on a c# winform application. There i am using geckofx geckoWebBrowser to get some html values. From following html i want to grab text- Super Deluxe Round Silver Above Ground Winter Pool Cover but you can see what i have already tried to grab that text and that not works. Any idea whats wrong i am doing? How to fix?
c#:
url = #"https://www.homedepot.com/s/0723815359971";
geckoWebBrowser1.Navigate(url);
DateTime now = DateTime.Now;
do
{
this.Refresh();
Application.DoEvents();
} while (now.AddMilliseconds(5000) > DateTime.Now);
GeckoHtmlElement element = null;
var geckoDomElement = geckoWebBrowser1.Document.DocumentElement;
if (geckoDomElement is GeckoHtmlElement)
{
element = (GeckoHtmlElement)geckoDomElement;
innerHtml = element.InnerHtml;
title = element.GetElementsByTagName("pod-plp__brand-name")[1].NodeValue;//this is what already tried but not works
if (title != "")
{
MessageBox.Show(title);
}
}
Html:
<a class="" data-pos="0" data-request-type="sr" data-pod-type="pr" href="/p/Swimline-16-ft-x-16-ft-Round-Silver-Above-Ground-Super-Deluxe-Winter-Pool-Cover-SD12RD/305609609">
<span class="pod-plp__brand-name">Swimline</span>
Super Deluxe Round Silver Above Ground Winter Pool Cover
</a>
GetElementsByTagName gets elements by the HTML tab name. (eg. a, span, div etc.)
Something like this ought to do it (assuming single 'a' in your document):
element.GetElementsByTagName("a")[0].FirstChild.NextSibling
Which gets the second element in the 'a', if that what you want.

How radio button click if included onclick function?

<asp: label input name="id" value="p" onclick="form.submit();
return false;" type="radio">personal / romance
/label>
I am trying below both code but not getting any solution. Please if anyone have any good solution then it's really good help for me.
webBrowser1.Document.GetElementById("id").SetAttribute("value", "P");
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("label"))
{
if (el.InnerText == ("personal romance"))
{
el.InvokeMember("Click");
}
}
Your code looks okay, but the InnerText of the label is not personal romance
Try it like this:
string searchString = #"personal / romance";
if(el.InnerText.Equals(searchString)
{
el.InvokeMember("click");
}
Check when expect this element if the correct tag name is label. I can tell you for sure that this is not asp:label. You can check with Inspect Elements in Chrome, if the tag name is different use correct one when you are taking webBrowser1.Document.GetElementsByTagName(correct tag)

How do I click a button that has the same class as 10 others?

I have a webpage that I'm trying to click a button on.
This is a couple of the webpage button codes:
<div class="ContentTab">My Bets</div>
<div class="ContentTab">Chat</div>
<div class="ContentTab">Account</div>
It only has the innertext different on them.
My goal was to click the "Chat" tab with this code, but it doesn't work.
foreach (HtmlElement chat in wb.Document.GetElementsByTagName("input"))
{
if (chat.InnerText == "Chat")
{
chat.InvokeMember("Click");
loggedIn = true;
break;
}
}
Does anyone know how to do it correctly?
Thanks
You are searching for input tags with this code:
foreach (HtmlElement chat in wb.Document.GetElementsByTagName("input"))
But all your elements are declared as div:
<div class="ContentTab">My Bets</div>
<div class="ContentTab">Chat</div>
<div class="ContentTab">Account</div>
Making these consistent will fix your problem.

How do I programmatically click on a div in a web browser?

I have been trying for a few days now and still have had no success in programmatically clicking on this div. All of the other input fields and buttons are working fine using InvokeMember("click") and RaiseEvent("onclick"), but I am unable to click on the following div:
<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>
This div is repeated several times on a page, but I just want to click on the first occurrence.
This is what I have done so far:
HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("class").Contains("pump request"))//also this condition is not returning true
{
e2.RaiseEvent("onclick");
}
}
#bleepzter
what if "somecontrol" is a class of the div instead of div's id?
since in my case i have div class "pump request" so (if i write "pump request" as somecontrol in above example) it return me Null in someDiv
<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>
#Cameron
yep i did entered the break; but the problem is the if condition never returns true so
HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
foreach (HtmlElement e2 in c1)
{
if (e2.GetAttribute("class").Contains("pump request"))//--> This condition is not returning true
{
e2.RaiseEvent("onclick");
break;
}
}
#Ilya Kogan
yea i just did a watch on e2.GetAttribute("class") and the weird thing happened that being reading the actual div (which i want to click) the value of class was empty :-o
try this one
if (e2.GetAttribute("className").Contains("pump request"))
{
e2.InvokeMember("Click");
}
you can try this piece of code by using a web browser control.
// browser is the web browser control
HtmlElementCollection col = browser.Document.GetElementsByTagName("div");
foreach (HtmlElement helemnt in col)
{
if (helemnt.InnerText !=null && helemnt.InnerText=="something")
{
helemnt.InvokeMember("Click");
break; // break the loop
}
}
It is simple. Here is an example which assumes your browser control is called browser, and the div you are looking for is called somecontrol (i.e. the id of the div is somecontrol):
HtmlElement someDiv = browser.Document.All["somecontrol"];
object someDivElement = someDiv.DomElement;
MethodInfo clickMethod = someDivElement.GetType().GetMethod("click");
clickMethod.Invoke(someDivElement, null);
All this is possible via reflection.

Categories