How to find if a text exists on page using selenium
java, selenium, selenium-webdriver
Solution
Try this code:
The below code used to check the text presence in the entire web page.
if(driver.getPageSource().contains("your Text"))
{
//Click xyz
}
else
{
//Click abc
}
If you want to check the text on a particular web element
if(driver.findElement(By.id("Locator ID")).getText().equalsIgnoreCase("Yor Text"))
{
//Click xyz
}
else
{
//Click abc
}
Problem
If the text exists then click on `xyz` else click on `abc`. I'm using the below `if` statement: ``` if(driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]")).isDisplayed()) { driver.findElement(By.linkText("logout")).getAttribute("href"); } else { driver.findElement(By.xpath("/html/body/div/div/div/a[2]")).click(); } ``` Script fails with the following error message: ``` Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]"} ```