XPath “Contains”Method - Selenium Automation
One of the most used xpath function is contains, Because many times
we won’t find the unique attributes like id or class name etc.. but we
will be knowing button name or text, in that case we can use contains
method.
Contains:
By using 'contains' function in XPath, we can extract all the
elements which matches a particular text value.
Example:
in the below, I’m finding the “Selenium Tutorial” menu link with using
text “Selenium Tutorial”
//a[contains(text(),'Selenium Tutorial')]
We can use contains method if we know partial text also like below
example:
//a[contains(text(),'nium Tut')]
Complete Code with contains example in Selenium:
package tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ContainsXpathDemo {
static WebDriver driver;
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "path");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://mahantesh-hadimani.blogspot.com");
Thread.sleep(5000);
driver.findElement(By.xpath("//a[contains(text(),'Selenium Tutorial')]")).click();
Thread.sleep(5000);
driver.close();
}
}
No comments:
Post a Comment
If any suggestions or issue, please provide