how to get the input element by class using mshtml.IHTMLDocument2 - c#

I'm new in using mshtml package library from .NET and, as such, I'm struggling to read the input element rendered in WPF web browser control.
I have researched through the internet and stumbled on this link. How to get the value of an input box of the webbrowser control in WPF?
But in my case, I want to read/get the input element by class, not by name attribute. Unfortunately, I know that GetElementByClass is not an option in WebBrowser.Document
This is my code - When I tried to run the debugger, the code doesn't continue inside the foreach loop ):
private void browser_LoadCompleted(object sender, NavigationEventArgs e)
{
IHTMLElementCollection elementCollection;
IHTMLDocument3 dom = (IHTMLDocument3)browser.Document;
elementCollection = dom.getElementsByTagName("input");
foreach (HtmlElement item in elementCollection)
{
if (item.OuterHtml.Contains("input_search"))
{
//Do something else here...
}
}
}
A screen capture of the DOM of the page
Any help is greatly appreciated.

Related

C# Click on HtmlElement With no ID only Class

I am not using selenium nor anything else, i just want to do it on the webbrowser on the windows form application.
I have a windows form application and i want to click on a button with code but there is no ID.
I tried using a lot of different things found on this websites forums, but none of this works.
Have you tried using WebBrowser.GetElementByTagName("div") and then checking each element against attribute type=submit?
Your code should look something like
HtmlElement submit = FindSubmitElement(webBrowser1.Document);
submit?.InvokeMember("submit");
public HtmlElement FindSubmitElement(HtmlDocument document)
{
HtmlElementCollection elems = document.GetElementsByTagName("div"); // since your tag is div
// this will return collection, even in case there is just one div, find the first one, having an attribute 'type' with value 'submit'
foreach (HtmlElement elem in elems)
{
string type = elem.GetAttribute("type");
if (!string.IsNullOrEmpty(type) && type == "submit")
{
return elem; // if div tag with attribute type is found exit and return that html element
}
}
return null; // if no div tags found with an attribute 'type' return null
}
Check more on GetElementsByTagName method on the MSDN docs. Code is taken from there and adjusted to your need.

WinForms Control Not working

I am trying to read in the first description in the first row. However, for some reason my if statement is never becoming true. Here is the html code that has the class im trying to read from. I gave a picture of it. Below that is the actual code im running. There is a first page that I login to and then input the part number. This then takes me to this page where you see the html code in the picture. I then need to grab the description of the part number. I included a picture of the website I am grabbing the description from. In order to give you a better Idea of what's happening. Also, I have a web browser component in my code that I am using. Can you point out why its not working? Thanks.
var secondPage = webBrowser1.Document;
foreach (HtmlElement element in secondPage.All)
{
if (element.GetAttribute("className") == "SE-Content-PartSearch-Grid-Row-Description")
{
messagebox.Show("Found it");
}
}
ActualWebsite
HTMLCODE
I suggest that you put a breakpoint on the "if" statement. It will perhaps be easier to see what is going on if you modify the code slightly:
var secondPage = webBrowser1.Document;
foreach (HtmlElement element in secondPage.All)
{
String className = element.GetAttribute("className");
if (className == "SE-Content-PartSearch-Grid-Row-Description")
{
messagebox.Show("Found it");
}
}

Selenium webdriver: IE 11 element.Click() doesnt work

I tried to find any solution but nothing is not helped me.
I have this element
<span data-lkd="GUI-411396" data-lkta="tc" data-lkda="title" class="panelbar_item" title="Hledat">Form</span>
In Selenium I find it with
IWebElement form = GetElementAndWaitForEnabled(By.CssSelector("span[data-lkd=\'GUI-411396\']"));
It's not problem to this part. But if try click on this element in IE11 nothing happend
find.Click()
I tried some solution like:
driver.SwitchTo().Window(driver.CurrentWindowHandle);
find.SendKeys(Keys.Enter);
find.Click();
But nothing happend. In Chrome and Firefox is normaly click on element.
If I clik in other elements for example button it works on IE 11. But I need click on this element.
I'm using Selenium v2.46.0, IE 11 (x86, x64).
With IE, it's always something extra you should do. Try this "special" trick:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", find)
It looks like you are trying to click on a span element. Instead of using a work around to click on the span element and trying to get the desired effect, try checking to see if it is wrapped in an anchor element or input / button element.
As an aside a good practice is to remember to always scroll the element into view, an example wrapper function would be:
public static void clickElementAsUser(WebDriver driver, By by)
{
WebElement element;
try
{
element = driver.findElement(by);
scrollElementIntoView(driver, element);
Thread.sleep(100); //Wait a moment for the element to be scrolled into view
element.click();
}
catch(Exception e) //Could be broken into multicatch
{
//Do Something
}
}
public static void scrollElementIntoView(WebDriver driver, WebElement element)
{
try
{
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
catch(Exception e)
{
//Do Something
}
}
If you post a small code sample of what is aroudn the span I may be able to help further. Goodluck!

how to get xml data to listbox

ive got a problem with getting my data from a xml file to an listbox.
this is the data i want to get in my listbox:
<gjester>
<gjest>
<id>test</id>
<fornanv>test</fornanv>
<etternavn>test</etternavn>
<adresse>test</adresse>
<telefonnr>test</telefonnr>
</gjest>
</gjester>
and i created a listbox in my gui. But i don't know what to write in my code.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
I don't know what to write here
}
There are approximately a bazillion ways to add items from an XML file to your listbox but a good place to start would be the MSDN documentation for the XMLTextReader class and the ListBox.Items.Add() method.
Also - you will probably want to do this somewhere other than the SelectedIndexChanged event on the listbox. For learning purposes, try it on a button click.
Good luck - and after looking into the above I'm sure someone will help you if you haven't figured it out.
This is .NET 4.0 (VS2010 C#) and is completely untested but may give you a start....
private void FillListBoxWithThingsIWantToSelect()
{
XDocument ListBoxOptions = XDocument.Load(Filename);
foreach (XElement element in ListBoxOptions.Root.Elements())
{
if (element.Name.LocalName.Contains("gjester"))
{
foreach (XElement subelement in element.Elements())
{
if (subelement.Name.LocalName.Contains("gjest"))
{
// What do you want to add? The Attribute? Element value
listbox1.Items.Add(element.Value.ToString());
}
}
}
}
}
Would help if you listed your platform and what you want in the listbox.
You want to call this from your constructor.
Can use dictionary object to bind the data from XML to Listbox.
var dic = (from order in ds.Tables[0].AsEnumerable()
select new
{
UserView = order.Field<String>("Value"),
DevView = order.Field<String>("id")
}).AsEnumerable().ToDictionary(k => k.DevView, v => v.UserView);
Click here for reference

C# Navigate to Anchors in WebBrowser control

We have a web browser in our Winforms app to nicely display history of a selected item rendered by xslt.
The xslt is writing out <a> tags in the outputted html to allow the webBrowser control to navigate to the selected history entry.
As we are not 'navigating' to the html in the strict web sense, rather setting the html by the DocumentText, I can't 'navigate' to desired anchors with a #AnchorName, as the webBrowser's Url is null (edit: actually on completion it is about:blank).
How can I dynamically navigate to Anchor tags in the html of the Web Browser control in this case?
EDIT:
Thanks sdolphion for the tip, this is the eventual code I used
void _history_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
_completed = true;
if (!string.IsNullOrEmpty(_requestedAnchor))
{
JumpToRequestedAnchor();
return;
}
}
private void JumpToRequestedAnchor()
{
HtmlElementCollection elements = _history.Document.GetElementsByTagName("A");
foreach (HtmlElement element in elements)
{
if (element.GetAttribute("Name") == _requestedAnchor)
{
element.ScrollIntoView(true);
return;
}
}
}
I am sure someone has a better way of doing this but here is what I used to accomplish this task.
HtmlElementCollection elements = this.webBrowser.Document.Body.All;
foreach(HtmlElement element in elements){
string nameAttribute = element.GetAttribute("Name");
if(!string.IsNullOrEmpty(nameAttribute) && nameAttribute == section){
element.ScrollIntoView(true);
break;
}
}
I know this question is old and has a great answer, but this hasn't been suggested yet, so it might be useful for others that come here looking for an answer.
Another way to do it is use the element id in the HTML.
<p id="section1">This is a test section</p>
Then you can use
HtmlElement sectionAnchor = webBrowserPreview.Document.GetElementById("section1");
if (sectionAnchor != null)
{
sectionAnchor.ScrollIntoView(true);
}
where webBrowserPreview is your WebBrowser control.
Alternatively, sectionAnchor.ScrollIntoView(false) will only bring the element on screen instead of aligning it with the top of the page

Categories

Resources