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')]"));

2) starts-with

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

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




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.