0% found this document useful (0 votes)
82 views12 pages

Element Locators Strategies in Selenium

This document discusses different strategies for locating elements in Selenium, including standard identifiers, XPATH, CSS locators, links, partial links, and DOM. It provides examples of each strategy, such as using attributes, relative paths, position, and contains keywords in XPATH, and pseudo elements and partial matching in CSS locators. The strategies allow Selenium to identify specific HTML elements for commands.

Uploaded by

salman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views12 pages

Element Locators Strategies in Selenium

This document discusses different strategies for locating elements in Selenium, including standard identifiers, XPATH, CSS locators, links, partial links, and DOM. It provides examples of each strategy, such as using attributes, relative paths, position, and contains keywords in XPATH, and pseudo elements and partial matching in CSS locators. The strategies allow Selenium to identify specific HTML elements for commands.

Uploaded by

salman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Element Locators Strategies in Selenium

Element Locators tell Selenium which HTML element a command refers to.

There are several strategies to locate Elements in selenium. Selenium support the
following strategies for locating elements.

• Standard identifier
• XPATHS
• CSS Locators
• Link
• Partial Link
• Dom(Javascript Expression)
Standard identifier
• id=‘first_name’: Select the element with the specified @id attribute.

• name=‘last_name’: Select the first element with the specified @name attribute
XPATH
•xpath=xpathExpression: Locate an element using an XPath expression.

There are following way to write a xpath expressions


1.Xpath expressions using attributes of element.

2.Xpath expressions using Relative path to reach element.

3.Xpath expressions using position element


XPATH expressions using attributes of element.
•Using class Attribute : xpath=‘//input[@value='Create Account']’

Using href Attribute: xpath=‘//a[@href='/category/Animals-Wildlife-1/']’


Xpath expressions using Relative path to reach element

• xpath=‘//td[@class='has-account plx']//a[@href='/login.html']’

• xpath=‘//div[@id='wrapper']/div[@class='bt mbm ptm']/h3’


Xpath expressions using position element
• Xpath=‘//ul[@id='clip-related-categories']/li[2]/a’

• xpath=‘//div[@id='clip-related']/a[3]/img’
XPATH using ‘contains’ keyword
•contains using partial match of attributes of a element:-
xpath=‘//div[@class='shadow_gray language-menu']//a[contains(@href,'language=fr')]’

•Contains using partial match of Text


xpath=‘//a[contains(text(),'purchase')]’
Navigation from child to parent and siblings in xpath

•Xpath=‘//nobr[contains(text(),'Purchase')]/../..//td[@class='order-summary-title']/b’

Sibling in Xpath
•Xpath=‘//form[@id='main-form']/following-sibling::a’
Xpath using ‘AND’, ‘OR’, & ‘LAST’

• xpath=‘//tr[contains(@id,'subscription-row-25') and contains(@class,'subscription-


row')]//span[text()=“25"][last()]’

• xpath=‘//div[@id='container' or @class='footer-link-container']//a[contains(@href,'faq')]’
CSS Locators
•Css selector locator supports all css1, css2 and css3 selectors but some namespace in css3, some pseudo
classes are not supported by selenium. These are given below
namespace:--
:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-
type, :visited, :hover, :active, :focus, :indeterminate)
pseudo elements:--
::first-line, ::first-letter, ::selection, ::before, ::after

Examples of CSS Locators : ---


css=li[id='user_options_selector']>a>span

css=div.rebill-continue a[href='/']
•Partial match of attribute in css locators
css=a[href*='/auto_rebill_status']

Sibling in CSS Locators:


Css=input[class='button button_small green_button mrm']+span

•Css selector using pseudo element


Css= div.msg div:nth-child(2)
Locate Element using ‘Link’
•link=Enter VAT to remove charge

•Locate Element using ‘ Partial Link’


driver.findElement(By.partialLinkText("Create a Free Browse"));

Locate Element using ‘Dom ’ on selenium RC.


In Web driver ,You can execute arbitrary JavaScript to find an element and as long as you
return a DOM Element, it will be automatically converted to a WebElement object.

dom=document.forms['subscribeForm'].elements[11]

You might also like