如何查找页面上是否存在文本 [英] How to find if a text exists on page

查看:133
本文介绍了如何查找页面上是否存在文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文本存在,则单击 xyz 否则单击 abc

If the text exists then click on xyz else click on abc.

我使用以下 if 声明:

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]"}


推荐答案

试用此代码:

以下代码用于检查整个网络中的文字存在页面。

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
}

这篇关于如何查找页面上是否存在文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆