WAYS TO FIND DYNAMIC XPATH FUNCTIONS IN SELENIUM WEBDRIVER


XPATH FUNCTIONS IN SELENIUM WEBDRIVER

  • XPath:  //button[starts-with(@id, 'continue')] 
  • XPath: //input[contains(@class, 'suggest')].
  • XPath: //input[@id='email']/parent::*
  • XPath: //input[@id='email']/following::*
  • XPath: //input[@id='email']/following::tr
  • XPath: //select[@id='month']/following-sibling::*
  • XPath: //select[@id='month']/following-sibling::select/
  • XPath: //input[@id='pass']/preceding::tr
  • XPath: //select[@id='day']/preceding-sibling::select/
  • XPath://select[@id='day']/preceding-sibling::*
  • //*[contains(@id,’your-common-pattern’)]
Using CSS we can use as below:
  • With ID - css=input#email or css=#email
  • With Name - css=input[name=email] or css=[name=email]
  • xpath : - //*[@id='email' or @name='email']
  • xpath: //input[@name='email'][@style='background-color: transparent;']
Some MoreExamples using Xpath Functions:  I often use "contains", but there are more. Here are some examples:
  • Multiple Condition: //div[@class='bubble-title' and contains(text(), 'Cover')]
  • Partial match: //span[contains(text(), 'Assign Rate')]
  • Starts-with: //input[starts-with(@id,'reportcombo')
  • Value has spaces: //div[./div/div[normalize-space(.)='More Actions...']]
  • Sibling: //td[.='LoadType']/following-sibling::td[1]/select"
  • More complex: //td[contains(normalize-space(@class), 'actualcell sajcell-row-lines saj-special x-grid-row-collapsed')]
  • Case sensitive : By.xpath("//td[contains(text(),'youruser')]") //here user text is case sensitive
  • By.xpath("//td[contains(lower-case(text()),'youruser')]") //to handle case sensitivity. Here user is not case sensitive
Lets we take sample Example here:

Using single attribute

Syntax
  • // tagname[@attribute-name=’value1’]
Example
  1. // a [@href=’http://www.google.com’]
  2. //input[@id=’name’]
  3. //input[@name=’username’]
  4. //img[@alt=’sometext’]
Sample Example:- .//input[@id='Email'][@name='Email'] – Example for finding Email field in Gmail.

Using contains method

Syntax
  • //tagname[contains(@attribute,’value1’)]
  • //input[contains(@id,’’)]
  • //input[contains(@name,’’)]
  • //a[contains(@href,’’)]
  • //img[contains(@src,’’)]
  • //div[contains(@id,’’)]
Sample Example:- .//*[contains(@id, 'link-signup')] – Example to find out links

Using multiple attribute

Syntax
  • //tagname[@attribute1=’value1’][attribute2=’value2’]
  • //a[@id=’id1’][@name=’namevalue1’]
  • //img[@src=’’][@href=’’]
Using starts-with method

Syntax
  • //tagname[starts-with(@attribute-name,’’)]
  • //id[starts-with(@id,’’)]
  • //a[starts-with(@href=’’)]
  • //img[starts-with(@src=’’)]
  • //div[starts-with(@id=’’)]
  • //input[starts-with(@id=’’)]
  • //button[starts-with(@id,’’)]
Using Following node

Syntax
  • Xpath/following::again-ur-regular-path
  • //input[@id=’’]/following::input[1]
  • //a[@href=’’]/following::a[1]
  • //img[@src=’’]/following::img[1]
Using preceding node

Syntax
  • //input[@id=’’]/ preceding::input[1]
  • //a[@href=’’]/ preceding::a[1]
  • //img[@src=’’]/ preceding::img[1]

Comments

Popular posts from this blog

Native Android Apps Automation with WebDriver Using Appium Mobile Automation tools

Appium Automation tool Desired Capabilities to run on Real Device

Handling IE Protected Mode issue/IE Security issues in selenium WebDriver