Search for a button by its HTML Code - c#

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();");

Related

c# selenium chrome-webdriver write advanced Path (chrome-driver)

I just try to write correct path to click exactly this button what I want on my page. I will give you an example page for testing. You just have to download the HTML file and open it in your browser.Code of HTML page
What we now want to do?
If you run this HTML file you will see all page.
And now we want to make a Click on exactly this button on screen :
After when you click on this button you will see click counter below: like this :
Have anyone idea how to click it? I tried few ways and can't find solution still. Please help.
I try at least :
drive.FindElement(By.XPath("//tr[class='ng-scope']/td[text()='Wylaczenie nadan RDF'] and button[#title='Skip']")).Click();
and
drive.FindElement(By.XPath("//tr[text()='Wylaczenie nadan RDF']/button[#title='Skip']")).Click();
+ more and more and can't just write fine this Path to click exacly this button.
And unique value is <td class="ng-binding">Wylaczenie nadan RDF</td> - i prefer to get path by this.
You need to use below XPATH for the same
//tr[td='Wylaczenie nadan RDF']//button[#title='Skip']"
//tr[td='Wylaczenie nadan RDF'] will take you to the row which contains that text and then you use //button[#title='Skip'] to find that button

Press a button using class names in c# dom

So basically I want to press a button in a website and i dont know what code to use. The HTML line looks like this:
Click Here
I want the program to click on that Free Button using DOM. I tried this :
webBrowser1.Document.GetElementByClassName("free").InvokeMember("click");
But for some reason the GetElementByClassName is highlighted (in visual studio 2015)
I cannot change the HTML since the website is not mine.
It's highlighted cause there is no function GetElementByClassName in WebBrowser.Document!
Look at this, maybe it would help you:
How to select a class by GetElementByClass and click on it programmically

How to implement an if else condition in the code behind for ImageButton OnClick

I am working on my website design and have already implemented a simple slideshow consisting of image buttons that would redirect users to different pages when they click on them. However the issue that I've noticed is that all of the images redirect users to the same page and I realized it was due to all the Images in the slideshow inheriting the method Response.Redirect("url")...then I tried to use an if else condition for example
if(ImageButton3.ImageUrl=="/Images/Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}
However I have noticed that this method does not work. Do you guys have any suggestions, huge thanks in advance!!!
I would suggest you use a data-url attribute on your image buttons. When you post back you can get the url off the ((ImageButton)sender).Attributes("data-url") and redirect to that url.
Another suggestion, if you aren't doing anything else in the event handler, you could simply use links around images.
Did you try to debug this code? To see if it reaches the URL you specified?
You can do that by clicking on "Start with Debugging" in Visual Studio.
Another good bug ugly debugging method is to add an alert (MessageBox) inside the if sequence and see if it gets there. For example:
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", ("The URL is " + ImageButton3.ImageUrl));
if(ImageButton3.ImageUrl=="/Images/Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}
Edit: After seeing your comment, I suggest accessing this image like that (notice the #):
if(ImageButton3.ImageUrl=="#Images\Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}

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

Having trouble clicking on an element in an automated test

I am currently building a test harness for the company I work at. I have experience both with C# and WatiN and have never encountered the issue I am now having.
Below, is a snippet of the markup for the page giving me the issue:
<div id="toggle1" class="NavLayout toggle">
<span onClick="toggleMenu(1, false);">
<span id="toggletext1">Quote Processing</span>
</span>
</div>
As you can see, I have a div, 2 spans and an image. I am using WatiN to try and click the image, that will then expand the menu, exposing yet another layer that I will need to click something else on. The problem I am having is in getting the 'Click' to happen. From what I can see in the snippet, it seems to me I need to be able to click the event, but cannot 'find' it with the code.
Any help out there to be had?
I have also had issues with clicking on certain elements.
I've run into issues where I could only click on an element if it was highlighted by mousing over the element.
Since I cannot see your code snippet, I can't tell if there is any javascript that deals with mouseover associated with the image, but if there is, you can try the following:
img.FireEvent("onmouseover");
img.FireEvent("onmousedown");
img.FireEvent("onMouseup");
You might also might want to try img.FireEvent("onclick") as well.
These are all guesses, since I can't see your code. It's also possible that rather than clicking on the image element itself, that you may want to try clicking on the parent object.
EDIT:
Ok, now that I can see your code, it appears that you should fire an onClick event against the span with the 'onclick' code in it.
I don't see an image listed in your code snippet, but this code should call the parent of the lowest level span.
Watin.Core.Span span = browserinstance
.Span(Find.By("innertext", "Quote Processing"));
span.Parent.FireEvent("onclick");
The DOM content that you intended to post is not visible. You might want to edit your post and check if it is visible.
In order to click on images
Watin.Core.Image img = browserinstance.Image(Find By Constraint);
if (img!=null and img.Exists)
img.ClickNoWait();
OR
img.FireEvent("onclick");

Categories