C# GeckoFX Browser - How to click/set value to XPath? - c#

Im having problems with website automation, I am able to confirm if website contains XPath im looking for, but I have no idea how to make program to set text or click it.
Im using Firefox 22.0 / Xulrunner 22.0
Code I used to verify if website contains XPath:
GeckoNode element = gWB.Document.GetSingleElement(x.ToString());
if (element != null)
{
//What here to make program to click/fill XPath?
}
Thanks

To perform a click, cast the GeckoNode type to GeckoHtmlElement then you can call the click method.
GeckoHtmlElement element = (GeckoHtmlElement)gWB.Document.GetSingleElement(x.ToString());
if (element != null)
{
element.Click();
}
Setting the value can be different depending on the type of the element, but you can try and use attributes such as NodeValue, TextContent, and InnerHtml.

If you want to use XPath, you can try with this:
browser.LoadXml("<MyTag><div>helloworld</div></MyTag>");
var r = browser.Document.EvaluateXPath("//div");
Assert.AreEqual(1, r.GetNodes().Count());
so in prev code:
GeckoElementCollection nodes = browser.Document.EvaluateXPath(x.ToString()).GetNodes();
foreach(GeckoNode node in nodes)
{
//do whatever you need to do with the node ..
GeckoElement element = node as GeckoElement;
element.click();
//..
}

Related

Selenium Webdriver C# How to verify if an element and it's value which is a dynamic text is already visible?

How to verify if an element and it's value which is a dynamic text is already visible using Selenium Webdriver C#?
Note that I'm making some assumptions here about things your question isn't very specific on. I'm assuming the element is any kind of html tag and the "dynamic?" text you refer to is its' inner text.
From the top of my head:
Boolean visible = false;
IWebElement element = Driver.GetElement(By.Xpath(expression));
if (element is IWebElement && element.IsDisplayed())
{
if (element.Text == expectedText)
{
visible = true;
}
}

Set & Get input value in web browser in c#

I just try to understand the concept of in web browser control c# win app.
I have created a Windows app with web browser control. I have called a web page (which is my own) and try to set value in the web page from my app.
When i try with Google
webBrowser1.Navigate("http://google.com/search?q=" + "C#");
it works fine.
When i try in this way, it is not working.
HtmlElement textArea = webBrowser1.Document.All["q"];
textArea.InnerText = "dsfs";
Can anyone help me to achieve this?
You will need to wait for the WebBrowser to load before you access it (otherwise Document will be null until loaded) - subscribe a DocumentCompleted event handler for this. Also Document.All["q"] will return the first element with the name "q".
webBrowser1.Navigate("http://stackoverflow.com/questions/30431004");
webBrowser1.DocumentCompleted += (o, args) =>
{
var ele = webBrowser1.Document.All["q"];
if (ele.TagName.ToLower() == "input")
{
ele.InnerText = "dsfs";
}
};
If you want to change more than one such element, or if you want to locate elements by Id, Tag name etc, you will need to iterate the collection:
foreach (HtmlElement ele in webBrowser1.Document.All)
{
if (ele.TagName.ToLower() == "input")
{
ele.InnerText = "dsfs";
}
}

Get Firefox URL with UI Automation

I am trying to get the value of the URL in Firefox using the following code. The problem is it only returns "Search or enter address" (see tree structure with Inspect.exe below). It looks like I need to go one level down. Can someone show me how to do this.
public static string GetFirefoxUrl(IntPtr pointer) {
AutomationElement element = AutomationElement.FromHandle(pointer);
if (element == null)
return null;
AutomationElement tsbCtrl = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Search or enter address"));
return ((ValuePattern)tsbCtrl.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
For the tree structure, see:
It's not clear which element you are starting the search from, but you've got two elements with that name. One is a combo box control the other is an edit control. Try using using an AndCondition to combine multiple PropertyCondition objects:
var nameCondition = new PropertyCondition(AutomationElement.NameProperty, "Search or enter address");
var controlCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
var condition = new AndCondition(nameCondition, controlCondition);
AutomationElement editBox = element.FindFirst(TreeScope.Subtree, condition);
// use ValuePattern to get the value
If the search starts from the combo box, you could instead change TreeScope.Subtree to TreeScope.Descendants since Subtree includes the current element in the search.

How to select HtmlElement in WebBrowser

I`m having a webBrowser control with some span elements.
Now user clicks on one of them, I do some manipulations and after that I need to select clicked element in browser. How can I do this?
HtmlElement hitElement = exerciseTextEditorControl.Document.GetElementFromPoint(e.ClientMousePosition);
if (lastHitElement == null)
return;
// Some stuff elided
// Now need to make a selection of this element in web browser
I know I can use IHTMLTxtRange for selecting some text, but how can I do similar thing with HtmllElement?
Thanks in advance.
Found an answer. In case someone needs this as well:
public void SetSelectedElement(HtmlElement element)
{
IHTMLSelectionObject selection = HtmlDocument2.selection;
var htmlTxtRange = selection.createRange() as IHTMLTxtRange;
var iHtml = element.DomElement as IHTMLElement;
htmlTxtRange.moveToElementText(iHtml);
htmlTxtRange.select();
}

Block elements within site WebBrowser

I have created a WebBrowser object and would like to know how to block specific elements of a page such as a flash object. I presume I would need to check for an offending URL and cancel the navigation to that element.
Hmm I'd do something like this to disable the Flash elements
WebBrowser wb = new WebBrowser();
//...
//...
//...
//...
HtmlElementCollection hec = wb.Document.All;
foreach (HtmlElement element in hec)
{
if (element.TagName == "OBJECT")
{
element.Enabled = false;
}
}
You should inspect the WebBrowser.Document.HtmlDocument property to strip out any object tags responsible for displaying flash.

Categories