November 9, 2011

Element locators using Selenium

Element locator tells the element for selecting a particular object.


Element locator type= argument
command
target
value

We get the elements for selenium in HTML language.
While using selenium IDE we can get the auto generated element locator type with the following types:
i) Id or Identifier: @id refers the element of identifiers.
In Selenium, type the argument as id=sampleid


ii) Name: @name refers the element attribute for name
In Selenium, type the argument as name=samplename
    Name and Value is similar we can use both in same manner.
Name/value= samplename/value


iii) Link pattern:  In the HTML if any text is comes under the anchor ‘<a>’ tag then we use the link= textpattern

<a href="url">Link Text</a>
Example: link=Link Text

iv) Xpath Expression: Xpath always starts with //
If a element ID is changing continuously then we can use Xpath in its location for identifying element
For Example: Google new is changing continuously, In this scenario we need to use XPath Expression.
Example:
xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[4]/td[2]
xpath=//a[contains(@href,'#id1')]
xpath=//a[contains(@href,'#id1')]/@class
xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
xpath=//input[@name='name2' and @value='yes']
xpath=//*[text()="right"]

Get the link with the link text
<a href="url">Link Text</a>
-> link=Link Text

Get element with the element text
<a href="url">Link Text</a>
-> //a[text()='Link Text']
Get element with part of the element text
<a href="url">Link Text</a>
-> //a[contains(text(), 'ink Tex')]

Get element with an attribute
<a href="url">Link Text</a>
-> //a[@href='link url']

Get element with two attributes
<input value="”value”/" type="”text”">
-> //input[@type='text' and @value='value']

XPather is a firefox plugin help you to find the xpath.

iv) CSS: CSS locator which helps for assigning the CSS location to select the fields.
You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
css=a[href="#id3"]


Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).

v) DOM: it is a javascript expression. Finds the element in HTML document object model
locator is always starts with document.
dom=document.images[10]
dom=document.forms['myForm'].myDropdown
*******************************************

1. Direct Reference

Id=MyButton (note in this case, “Id =” is not even required)
Name=Description_Field
Value=ABC XYZ
Link=Contact Us

2. DOM or CSS

3. XPath References

3a. Simple XPath

//div(@class=’MyClass’)
//input[contains(@id, 'myTextField')]

3b. Complex XPath

/html/body/div[1]/div[5]/div/table/tbody/tr/td/p/a[3] (An example bad XPath – it will break as soon as the page layout changes a bit)

4. Click and Mouse Events

Click and ClickAt

MouseDown and MouseDownAt

MouseUp and MouseUpAt

MouseMove and MouseMoveAt

DragDrop (useful for moving slider bars that calls a JavaScript event to change a value when the slider is moved).

 

5. Keyboard and Coordinates

No comments:

Post a Comment