Selenium object identification - selenium

I am using Selenium webdriver to test my application & i am facing difficulties in identifiying button on the same. the code snippet is like :
<input type="submit" onclick="return sign(this);" value="Login">
and its xpath is :
html/body/table/tbody/tr[2]/td/center/form/center/table/tbody/tr[3]/td/center/input[1]
Which object property to use and how?

You should not use that XPath.
I would hazard a guess that you used some sort of tool, whether it's Firebug or IDE, to generate that XPath. Stop that now!
XPath is fine to use, and can be used here, just not relying on the tools to generate it for you! That XPath is destined for failure!
You will need to provide more HTML, specifically around that button.
However, you should just be able to use something as simple as:
//input[#value='Login']

You can use the xpath, if that is really stable. I found that it is much easier to define id tags in the html elements and the use a By.id locator. Alternatively you can use css selectors, depending on the "uniqueness" of your button something like this could work:
By.cssSelector("input[value='Login']")

Related

How to click on checkbox on webpage using selenium vba

I have a query,how to click on checkbox on webpage using selenium vba.
Below is the screen shot where i want to click
Below is the html code.
<span name="locSpans[]" value="Nerul" style="display:block">
<input type="checkbox" name="locArr[]" value="8897" onclick="enableDisableLocality(); showSelectedLoc();">Nerul
<br>
<input type="hidden" name="locArrVal[]" disabled="disabled" value="Nerul">
</span>
FindElementByCss is generally faster unless using IE, and then it depends which version of IE and what type of traversal is required.
Repeated tests have proven FindElementByCss to be more performant than FindElementByXPath (Note: that if there is a unique id present then selecting by id is always the first choice!)
In benchmarked tests Chrome and FireFox saw faster matching using CSS consistently across different traversal paths. They are optimized with CSS in mind and using CSS selectors is advocated as selenium best practice. IE was more variable with most instances of XPath being slightly more performant, but there being some clear paths that favoured CSS selection. Long XPath selectors will be costly and prone to breakage. Later versions of IE saw more variability. Opera12 browser came in with mixed results.
I would use a CSS selector:
So, for a simple selection based on likely unique attribute, I would go with an attribute CSS selector of [value='8897'] to target the value attribute. The [] means attribute selector. So value attribute with value of 8897.
driver.FindElementByCss("[value='8897']").Click
If you want to be more selective you can throw in an additional attribute selector, as follows, to target the type attribute.
driver.FindElementByCss("[type=checkbox][value='8897']").Click
When should I use XPath then?
Older IE versions for sure.
Any requirement for walking up the DOM would point to XPath usage.
XPath has some great additional locator strategies for hard to find elements, but that is not necessary AFAIK here. You can see some of the additional considerations here.
You can use xpath below to get checkbox, it means: find input with type="checkbox" and parent SPAN with text "Nerul".
driver.FindElementByXPath("//input[ancestor::span[normalize-space(.)='Nerul'] and #type='checkbox']").Click
Try this if not go for CSS Selector Option
bot.Window.Maximize
bot.FindElementByName("locArrVal").Click
bot.Wait 1000

How to identify button without Xpath in selenium

Code is below
<button type="submit" class="login-button">Login</button>
In selenium I tried below code:-
driver.findElement(By.classname("Login")).click();
please help me in this code without Xpath
Your classname is login-button not Login
driver.findElement(By.classname("login-button")).click();
You can use also partialLinkText
driver.findElement(By.partialLinkText("Login")).click();
partialLinkText is looking the Sub-String on HTML DOM
You can use also linkText
driver.findElement(By.linkText("Login")).click();
LinkText is looking the same String on HTML DOM
Using CSS-Selector
driver.findElement(By.cssSelector("button[class='login-button']")).click();
Hope it will help you :)
I always Prefer Cssselector rather than Xpath, it's up to the user to choose which they want and what they are comfortable with finding element.
The below link will be very useful if you want to know about CSSSELECTOR.
http://www.w3schools.com/cssref/css_selectors.asp
driver.findElement(By.cssSelector(".login-button")).click();
My suggestion would be Please inspect element and open console
$('.login-button')
Try this one until you get the required element you want. In that way you will be more flexible in getting the most required element.

Handling dynamic ids and classes

I am using selenium to test a web application, The ids and classes are always changing dynamically.So that I am not able to give correct identification, is it possible to get ids of the element in run time and is there any other method to handle this situation.
It depends on if ids are completely random or if there is some part of the id which remains the same. If yes, then cssSelector is the obvious choice
driver.findElement(By.cssSelector("div[id*=somePart]");
where id* means id contains. If you cant use this approach you will have to track down your element using xpath or again cssSelectors. XPath example is here and CSS selector could look like this
By.cssSelector("boyd table input");
I would strongly recommend locating elements by XPath -- with the caveat that you make your XPaths robust and not just "copy" the xpath using your browser's developer tools. XPath is very easy to learn. You can use XPaths to walk up and down the DOM, and identify elements by their text, or their attributes.
For example, maybe you need to click a button that has a span that contains the text that appears on the button:
<div class="btn-row random-generated-number-1234897395">
...
<button id="random-generated-number-239487340924257">
<span>Click Here!</span>
</button>
...
</div>
You could then use an xpath like this:
//div[contains(#class, 'btn-row')]//button/span[text()='Click Here!']/..
(The /.. at the end walks back up from the span to the button.)
XPath is powerful and flexible and easy to learn. Use it when the ids and classes aren't reliable.

Finding clickon Element using Selenium. (JAVA)

I spend hours already trying to find the way to find the Element using Selenium WebDriver. I assume I need to use driver.findElement(By.xpath("")), but I am not quite sure how.
I somehow need to find and click on "clickon" element. The problem is that part of that element is changing (see screenshot) I need to pick up from the file and putted into the xpath.
I would appreciate any help.
We have been rigorously searching for automated functional testing solutions recently, and we began with Selenium. The entire reason we decided to search for other solutions was that our application also has dynamic IDs with no other obvious XPath mechanism to identify them. Selenium is unable to identify these elements on the page without some additional knowledge, just as you would be unable to identify these elements on the page if you didn't already know what they are.
If you are controlling the DOM creation, consider adding a unique ID or class to this element.
We recently came across eggPlant from testPlant, and it is an interesting approach to functional testing. It's essentially image based. Other viable solutions are Ranorex or HP's QTP or SmartBear's TestComplete.
You can use xpath. If the div class is constant, you can use something like:
driver.findElement(By.xpath("list-row field-item")).click();
To view the xpath, you can install firefox plugin called 'xpath checker' found here and right click on the dom element and click 'View Xpath' option to get the xpath of the element and then you can use that xpath in your code.
Or you can even use regex in the xpath which is suitable for the similar problems. Xpath with regex is really powerful.
It seems that you want to click the div that has the on click attribute that contains certain text that doesn't change, ignoring the part that does. In that case, use an xpath like this:
//div[contains(#onclick, '/challenge/index/rfp_id/')]
This will select the first div with an onclick attribute with a value containing /challenge/index/rfp_id.

Selenium xpath flow

Can anyone tell me what is the exact flow of taking out xpath in Selenium-IDE.
After trying alot by putting alerts i m not getting how to take out the exact xpath.
Selenium displays the xpath according to xpath:position and some others ways also but i want to add the xpath traversing from html i.e the topmost position.How can i do that???
I've come to prefer CSS selectors over Xpath. The IDE won't create them for you, but with a little practice they're very easy to write. As a bonus, they make your tests much easier to maintain.
For example, let's say you have a button with the text "save". The locator would be:
css=button:contains('save')
Check out http://www.w3.org/TR/css3-selectors/ for more detail.
I use XPather to find out the xpath. Then I use the resulting path in the Selenium IDE to cross verify. You could also use other tools like XPath Checker or Firebug to do the same.
Write 1 line of code in javascript:
LocatorBuilders.order = ['id', 'link', 'name', 'dom:name', 'xpath:link', 'xpath:img','xpath:attributes', 'xpath:href', 'dom:index', 'xpath:position'];
change it the way you want (not all of them has to be there)
see:
chrome://selenium-ide/content/locatorBuilders.js
save as your_file.js
add your_file.js as "Selenium Core extensions" in the IDE options
Firefinder for Firebug helps matching both CSS and XPath locators
In general, with Firebug installed, you can Inspect Elements for their HTML and DOM structures to determine what best locator you can build.
the best way to find x PAth is :
open developer tool bar in chrome:
Find the x Path displayed below :
You can directly copy and paste this x path in your script