Handle Dynamic Elements using XPath in Selenium

In Selenium following XPath methods can be used to identify complex or dynamic elements in the web application:

1) contains

WebElement element = driver.findElement(By.xpath("//*[contains(@class,'alert')]"));
System.out.println(element.getText());

2) starts-with

WebElement element1 = driver.findElement(By.xpath("//*[starts-with(@class,'sufee-alert')]"));
System.out.println(element1.getText());

3) using or

driver.findElement(By.xpath("//*[@type='text' or @id='firstName']")).sendKeys("test1");

4) using and

driver.findElement(By.xpath("//*[@type='text' and @id='firstName']")).sendKeys("test1");

5) using text()

driver.findElement(By.xpath("//a[text()=' Sign in']")).click();

6) using index

driver.findElement(By.xpath("(//*[@id='gender'])[1]")).click();

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!