Click on element in Webbrowser - c#

I have about 10 of these tags under each other:
<a class="answer" href="#">This is one</a>
<a class="answer" href="#">This is Two</a>
<a class="answer" href="#">This is Three</a>
Now I need my webbrowser in C# to click on <a class="answer" href="#">This is Two</a>
How would I do this? Only got text and a tag with a class. No value this time.
Thanks

If you want it just for that "This is Two" link I believe it would go something like this:
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("This is Two"))
link.InvokeMember("Click");
}

Related

how i do perform click event using c# in webBrowser

How I do perform click event using c# in webBrowser.
This command is not working in c#, I don't know why.
webBrowser1.Document.InvokeScript("document.getElementsByName(\\"submitAddressButton\\")[0].click()");
But,
When I tried in web console. seem that command is work.
document.getElementsByName("submitAddressButton")[0].click();
I use getElementByName in c#, because there is no ID in HTML element target.
Below is fragmented of HTML :
<form name="addressSelectForm" action="/online_customers/page/manageaddress/site_qualification/
6d5b51984c1b4a73aaf722f01474a256/select_address?submitToken=0980629f4dd64f1d9e72b05e16281d9b" method="post">
<span style="display:none">LOCXXXXX51</span><input type="submit" class="link-button" name="submitAddressButton"
value="LOCXXXXX51">
<input type="hidden" name="fsaId" value="">
</form>
Thank
Jigu
HtmlElementCollection d0cument = homeBrowser.Document.GetElementsByTagName("button");
foreach (HtmlElement link in d0cument)
{
String class2 = link.InnerText;
if (class2 == "Login")
{
link.InvokeMember("click");
logincheck.Enabled = true;
}
}
i use like this because homeBrowser is have a lot of button

How getAttribute("onclick")

For example I have this html code:
<a href="#" onclick="return Index.submit_login('server_pl80');">
<span class="world_button_active">Świat 80</span>
</a>
And I need to get attribute of onclick, because I may get more links and I must find difference between them. For my opinion get attribute of onclick is only one way.
But if I GetAttribute("onclick") from HTMLElement it will return System.__ComObject.
Is there any idea how read onclick value from webBrowser?
I have only this:
HtmlElement selected_div = #webBrowser1.Document.GetElementById("div_id").GetElementsByTagName("div")[0];
HtmlElement a = selected_div.GetElementsByTagName("a")[0];
string rightLink = a.GetAttribute("href");
string onclickLink = a.GetAttribute("onclick"); // return "System.__ComObject" string
if (rightLink == "http://www.example.com/#")
a.InvokeMember("click", null);
"onclick" or "onClick" in getAttribute(); doesn't make a difference
This code works but click on first link on the list of servers.. I need choose the server and links have difference only in onclick attribute.

C# Access tags properties by HtmlElement

There is an html like:
<div id="instance" style="color:red;display:block; .... bila bila">
<h2>some text</h2>
</div>
I wanna access style of div via this code;
foreach (HtmlElement link in webBrowser1.Document.GetElementsByTagName("div"))
{
if (link.GetAttribute("id").ToString() == "instance")
{
MessageBox.Show(link.innerhtml);
}
}
But link.innerhtml gives me the inside of div tag, not div's own. Output text of Messag.Box is:
<h2>some text</h2>
I also tried this too:
MessageBox.Show(link.GetAttribute("style"));
but it didnt work.
How to access div properties via same div's id?
You should probably use something like this:
MessageBox.Show(link.OuterHtml);

check the url of current page in if statement

so i have this button that appears on multiple pages all leading to the parent page. But now - when i reach the parent page i still have the button showing. I want to remove/hide the button when on the parent page.
Is it possible that i could check my current page url and check to see if it the parent page?
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
thinking using an if statement surrounding the tag
If you wanted to do your check in the code behind page, you can always execute the code below during the page load event.
if (Path.GetFileName(Request.Url.LocalPath) == "ParentPage.aspx")
{ element.visible = false; }
else
{ element.visible = true; }
yeah that's what the window.location variable is for see this
When you are in your parent page, what is the ElementId value ?
I assume it can be null, so you may be able to check this value before displaying your button
#if(ViewBag.ElementId != null){
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}
else { //TODO...
}
It looks like ASP.NET MVC to me.
#if (!Request.Url.AbsolutePath.EndsWith("/i/dont/want/this"))
{
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}

C# - How to change HTML elements attributes

My master page contains a list as shown here. What I'd like to do though, is add the "class=active" attribute to the list li thats currently active but I have no idea how to do this. I know that the code goes in the aspx page's page_load event, but no idea how to access the li I need to add the attribute. Please enlighten me. Many thanks.
<div id="menu">
<ul id="nav">
<li class="forcePadding"><img src="css/site-style-images/menu_corner_right.jpg" /></li>
<li id="screenshots">Screenshots</li>
<li id="future">Future</li>
<li id="news">News</li>
<li id="download">Download</li>
<li id="home">Home</li>
<li class="forcePadding"><img src="css/site-style-images/menu_corner_left.jpg" /></li>
</ul>
</div>
In order to access these controls from the server-side, you need to make them runat="server"
<ul id="nav" runat="server">
<li class="forcePadding"><img src="css/site-style-images/menu_corner_right.jpg" /></li>
<li id="screenshots">Screenshots</li>
<li id="future">Future</li>
<li id="news">News</li>
<li id="download">Download</li>
<li id="home">Home</li>
<li class="forcePadding"><img src="css/site-style-images/menu_corner_left.jpg" /></li>
</ul>
in the code-behind:
foreach(Control ctrl in nav.controls)
{
if(!ctrl is HtmlAnchor)
{
string url = ((HtmlAnchor)ctrl).Href;
if(url == GetCurrentPage()) // <-- you'd need to write that
ctrl.Parent.Attributes.Add("class", "active");
}
}
The code below can be used to find a named control anywhere within the control hierarchy:
public static Control FindControlRecursive(Control rootControl, string id)
{
if (rootControl != null)
{
if (rootControl.ID == id)
{
return rootControl;
}
for (int i = 0; i < rootControl.Controls.Count; i++)
{
Control child;
if ((child = FindControlRecursive(rootControl.Controls[i], id)) != null)
{
return child;
}
}
}
return null;
}
So you could do something like:
Control foundControl= FindControlRecursive(Page.Master, "theIdOfTheControlYouWantToFind");
((HtmlControl)foundControl).Attributes.Add("class", "active");
Forgot to mention previously, that you do need runat="server" on any control you want to be able to find in this way =)
Add runat="server" on the li tags in the masterpage then add this to the appropriate page_load event to add the 'active' class to the li in the masterpage
HtmlGenericControl li = HtmlGenericControl)Page.Master.FindControl("screenshots");
li.Attributes.Add("class", "active");
You could register a client script like this:
(set id to the id of the li that you want to set to active)
ClientScript.RegisterStartupScript(this.GetType(), "setActiveLI", "document.getElementById(\""+id+"\").setAttribute(\"class\", \"active\");", true);
This generates a JavaScript call on the page near the bottom after elements have already been rendered.
All the parts have already been provided in previous answers, but to put the whole thing together, you'll need to:
add the runat="server" attribute to the <ul> and <li> elements
add a public method to do the work on the master page that can be called from the pages using the master page
call the method from the Page_Load of the pages
Alternatively you could also add the code to the OnLoad(...) method of the master page, so you don't have to add the method call to the Page_Load on every page.
If they were runat=server you could use the attributes property.
In order to find that particular control it will need to be defined as public (in the generated designer)
Or will need to be wrapped by a public get in the codebehind.
You can expose the li's on the master page to any content pages by wrapping them in properties on the master page:
public GenericHtmlControl Li1
{
get
{
return this.LiWhatever;
}
}
Then on the content page:
MasterPage2 asd = ((MasterPage2)Page.Master).Li1.Attributes.Add("class", "bla");
If i've got that right!
I found a link that works using CSS and involves only changing the body tag's class attribute. This means there's no Javascript and there's no for loops or anything.
#navbar a:hover,
.articles #navbar #articles a,
.topics #navbar #topics a,
.about #navbar #about a,
.contact #navbar #contact a,
.contribute #navbar #contribute a,
.feed #navbar #feed a {
background: url(/pix/navbarlinkbg.gif) top left repeat-x; color: #555;
}
....
<body class="articles" onload="">
<ul id="navbar">
<li id="articles">Articles</li>
<li id="topics">Topics</li>
<li id="about">About</li>
<li id="contact">Contact</li>
<li id="contribute">Contribute</li>
<li id="feed">Feed</li>
</ul>
Read more here
http://www.websiteoptimization.com/speed/tweak/current/
Try this
the great example for future use. i know this thread is old, but for future queries...
http://community.discountasp.net/showthread.php?p=33271
Thanks For the solution.
Mininized code.
Master page control can also find in the child page..
i mean master page contains html contol
and chilld page can find the master page html conrol like this
((HtmlControl)this.Master.FindControl("dpohome1")).Attributes.Add("class", "on");
Simple logic and minimal code, I usually use the following code, especially in dynamic menu. Hope this helps.
Create this method code in the code behind master page
CODE BEHIND (C#)
protected string SetCssClass(string page)
{
return Request.Url.AbsolutePath.ToLower().EndsWith(page.ToLower()) ? "active" : "";
}
In the menu list items you have created call this method passing the page name like this
HTML PAGE (ASPX inline code)
<li id="screenshots" class = "<%= SetCssClass("screenshots.aspx") %>">
Screenshots</li>
<li id="future" class = "<%= SetCssClass("future.aspx") %>">
Future</li>
and so on.
By this method, every time you add a page and link, you don't have to write code in every page. Just when you add the link in the master page, with every <li> invoke the SetCssClass(pagename) method call for setting class and it's done. (you can rename the method as per your ease.
You can use longer codes if you are being paid per lines of code bcoz then this is just one line of code. (lol). Just kidding. Hope it helps.
Note: I am ignoring other parts of the html code, you can include them also, that would work fine.

Categories

Resources