How to invoke click on a link in the web browser? - c#

Okay lets say the page contains a link called 'About Us'. Instead of me doing:
webBrowser.Navigate ("www.page.com/aboutus");
...how can I tell my program to click any link that contains the text About Us?
I know this might seem like Im doing more work then necessary I have too but trust me I need this bit of code.
Any help would be appreciated, thanks :)

You need to find the HtmlElement object for the <a> tag, then call InvokeMember("click").
If the element has an ID, you can get it by calling Document.GetElementById; otherwise, you can look through GetElementsByTagName and find the element you're looking for.

Related

Find and click a link with text - Coded UI

I am trying to create a test that finds a specified link on a web page and clicks it. What I am attempting to do is search for the link by specifying the name/text of the link. Is there a way to create a hyperlink object solely by specifying this?
I can do the following to find the link by specifying the href property like this:
BrowserWindow browser = BrowserWindow.Locate("Window Title");
var hyperlink = new HtmlHyperlink(browser);
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Href, "link.com");
Mouse.Click(hyperlink);
But I want to do the same thing by specifying the text/name of the link.
Any help would be appreciated! Thanks
I solved it...
This finds the link based on the text:
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "text");

Programmatically Clicking on a Web Page's Button in Windows Forms Application

I am working on a project which is Analysis of Papers from Google Scholar. What I do is basically, parsing the HTML, storing related fields into database etc. However, I am stuck at a point, while I am taking the Titles of the publications, I realized, I am able to get first twenty elements. But, there are sixty papers in related account:
http://scholar.google.com/citations?user=B7vSqZsAAAAJ
So, I think as a solution, I need to click to the 'show more' button programmatically, so I can have all the Title's, Publication Venue etc.
What do you think? How can I perform that kind of action?
Edit: I checked the 'show more' button, while there is nothing to show as a next page, its html code still remains same. As a solution I can use loop for n times. However, I am looking for more robust solution.
Thank you for your time!
If it is clicking on a button within a WebBrowser control on a Windows Form Application, then 'Yes' you can do it.
There are ways of getting more control over identification by using XPath.
(You might need to use Javascript to use XPath for object interactions - since you haven't asked for that, I will assume you don't need it)
webBrowser.Navigate("http://www.google.com");
// Or
HtmlElement textElement = webBrowser.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "your text to search");
HtmlElement btnElement = webBrowser.Document.All.GetElementsByName("btnG")[0];
btnElement.InvokeMember("click");
Or even typing into text boxes with
webBrowser1.Document.GetElementById("gs_tti0").InnerText = "hello world";
If its this website specifically, there is a simple workaround. Change the query string to what records you want.
http://scholar.google.com/citations?user=B7vSqZsAAAAJ&cstart=0&pagesize=2000

Search for a button by its HTML Code

I am writing Coded UI Test's for a web app. I am having problems such as I record actions using the test builder, however sometimes the button that is clicked has different information each time I run the test and as result VS can't find the button.
The html code never changes so what I want to do is find the button by its html code and click it that way.
For example on Google the path to the search button is
<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>
How can I click this button using the code above or Alternativly using the XPath
//*[#id="gbqfba"]
Any help would be greatly appreciated.
adjust the Search Properties and Filter Properties.
Something like:
$('button span').each(function(){
If($(this).html() == "Google Search")
{
$(this).parent('button').click();
}
});
I found the answer, If you go to the UIMap.uitest file and click the method you have the problem with you can change the filter properties and the search properties to remove parts that are failing your test.
This will do the trick:
BrowserWindow.ExecuteScript("$('#gbqfba').click();");

Read content from a URL, place into a textbox

I'm trying to read lines from a website and then, copy it into my textBox2.
textBox1 will have a website's URL like http://example.com.
When I click on button1 I'd like to read HTML content from the above URL, and please that info textBox2.
Should I use HtmlAgilityPack?
How can this be done?
2 =================================================
So, for example, i copy this link into my "textBox1"
http://www.mineshaftersquared.com/server/DareCraft#
So, is there a way to make app copy everything from:
//*[#id="Plugins"]
to
//*[#id="rightInfo"]/section[2]/div/div[1]/table[2]
/this is XPath
/It don't have to in XPath, but this was the only way I could show.
You don't install HtmlAgilityPack, you reference if from your project.
Read about WebClient class (OpenRead method), you probably want to use it to get the pages.
Here is a tutorial you might want to start with:
http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient

hover link info

how can i get the effect that when u point over a link an information is shown in a box which tells u what that link does.
its like when you hover over a link a box explains its action.
Can some1 help me out with a neat sample.
do i need to use the ajax - hovermenuextender for this??
Thanks
It depends on how fancy you want to be. I personally have found jQuery (AKA JavaScript) to answer the call here. qTip is a jQuery library that has some amazing pop-up "bubble" effects. Head over to their website and check out the various things you can do.
If you head to the qTip documentation (particularly this page), you can see how easy it is to replace "normal" link pop-ups with a much better looking tool tip. Code example shown below:
$('a[title]').qtip({ style: { name: 'cream', tip: true } })
Since you are using ASP.NET you could use ASPNET ToolTip; hover over the bulleted items in the link to see examples.

Categories