How to Identify Elements using XPath II using contains, starts with, ends with, text, Index II Selen

Опубликовано: 20 Октябрь 2024
на канале: Knowledge Share
186
6

In this session you will learn how to Identify elements using XPath functions

Please refer below session for CSS Selectors
   • How to Identify Elements using CSS Se...  

XPath - Part 1
   • Introduction to XPath and How to Iden...  


==================================================
Different ways of writing Dynamic XPath
==================================================
Basic
Standard Syntax : //tag name[@attribute='value']
2. AND Syntax : //tag name[@attribute1=‘value1’ and @attribute2=‘value2’]
3. OR Syntax : //tag name[@attribute1=‘value1’ or @attribute2=‘value2’]

Functions Related
4. Using contains Syntax : //tag name[contains(@attribute, 'value’)]
5. Using starts-with Syntax : //tag name[starts-with(@attribute, 'value‘)]
6. Using ends-with Syntax : //tag name[ends-with(@attribute, 'value‘)] Use CSS for ends with
7. Using text() Syntax : //tag name[text()=‘value’]
8. Using Index Syntax : (//tag name[@attribute='value'])[1] Simply (XPATH)[N]

===================
XPath Program :
===================
package com.seleniumbasics;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Locators_XPATH_functions_related
{

public static void main(String args[])
{


System.setProperty("webdriver.chrome.driver", "E:\\Selenium + Java - Youtube\\Selenium\\Libraries\\chromedriver_win32\\chromedriver.exe");
WebDriver driver =new ChromeDriver();
driver.get("http://demowebshop.tricentis.com/regi...");
driver.manage().window().maximize();
System.out.println("XPATH Examples - Registration Page ");

/* XPath using Functions, contains, starts-with , text , Index */

driver.findElement(By.xpath("//*[@id=\"gender-male\"]")).click();
driver.findElement(By.xpath("//input[contains(@id,'First')]")).sendKeys("knowledge123");
driver.findElement(By.xpath("//input[starts-with(@id,'Last')]")).sendKeys("share123");
driver.findElement(By.xpath("//input[starts-with(@id,'Email')]")).sendKeys("[email protected]");
Boolean check_password_label=driver.findElement(By.xpath("//label[text()='Password:']")).isDisplayed();
System.out.println("Password value presence : " + check_password_label);
driver.findElement(By.xpath("(//input[@type='password'])[1]")).sendKeys("123456");
driver.findElement(By.xpath("(//input[@type='password'])[2]")).sendKeys("123456");

System.out.println("this is for testing second push in to git repo");

}

}