Handle Dynamic Elements using XPath in Selenium

Learn how to handle dynamic elements using XPath in Selenium

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();

Bijan Patel
Full Stack Test Automation Expert | Selenium Framework Developer | Certified Tosca Automation Specialist | Postman | DevOps | AWS | IC Agile Certified | Trainer | Youtuber | Blogger|

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
QASCRIPT 2024 Privacy policy Terms of use Contact us Refund policy