Tips on CSS and XPath locators

CSS Locator

Note: Check the post’s locator exercise on this website, https://www.python.org/

Absolute CSS

  • A different way of handling absolute CSS; here, each forward arrow represents a child nest
"header[role='banner'] >>>>> input#id-search-field"

OR operator

  • Locate a single element with two different values on the same attribute, where, one of them can be invalid; say, `either-or` approach
"header[role='banner'], header[role='undefined']"
  • Locate multiple elements with different address path in the same statement
"[for='id-search-field'], input#id-search-field"

 

XPath Locator

Absolute XPath

  • A different way of handling absolute XPath
"//header[@role='banner']/..//input[@id='id-search-field']"

OR operator (separate definition)

  • Locate a single element with two different values on the same attribute, where, one of them can be invalid; say, `either-or` approach
"//*[@for='id-search-field']|//*[@for='undefined']"
  • Locate multiple elements with different address path in the same statement
"//*[@for='id-search-field']|//*[@id='id-search-field']"

OR operator (same definition)

  • Locate a single element with two different text values, where, one of them can be invalid; say, `either-or` approach
"//*[contains(@id,'id-search-field') or contains(@id,'undefined')]"
  • Locate multiple elements with attributes from two different address path
"//*[@id='id-search-field' or @for='id-search-field']"

AND operator (same definition)

  • Locate a single element with two different text values, where, both of them must be valid; say, `must-contains` approach
"//*[contains(@id,'id-search-field') and contains(@id,'search')]"

Text and tag

  • Usually, we locate an element with text contains; here, the text, Socialize and <a> tag is used to locate an element
"//*[a='Socialize']"

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s