如何检查页面源代码SeleniumJava中是否存在大文本

如何检查页面源代码SeleniumJava中是否存在大文本,java,selenium-webdriver,Java,Selenium Webdriver,如何检查页面源中是否存在大文本。 我使用了contains方法进行检查,但由于序列不准确,条件变为错误。 我想要的解决方案是如何只检查数据是否存在,而不干扰序列 public void demo() { String description="(Large data is present)"; String pagesource=driver.getPageSource(); //have also tried with String source = driver.findEl

如何检查页面源中是否存在大文本。 我使用了contains方法进行检查,但由于序列不准确,条件变为错误。 我想要的解决方案是如何只检查数据是否存在,而不干扰序列

public void demo()
{    
String description="(Large data is present)"; 
String pagesource=driver.getPageSource(); //have also tried with String    source = driver.findElement(By.tagName("body")).getText(); String    source = driver.findElement(By.tagName("html")).getText();    
if(pagesource.contains(description))
{ System.out.println("Match    Found"); }
else
{ System.out.println("Match Not Found"); }

}

似乎是字母大小写敏感问题,请尝试条件:

   if(pagesource.toLowerCase().contains(description.toLowerCase()))

    {

        System.out.println("Match Found");

    }
    else
    {
        System.out.println("Match Not Found");

    }

在上面,将两个字符串的所有字符转换为小写,然后进行比较。

如果希望在特定位置交叉检查数据,最好使用gettext();下面的例子将在两方面帮助您

WebDriver driver=new FirefoxDriver();
    driver.get("http://seleniumtrainer.com/components/buttons/");

    driver.findElement(By.id("button1")).click();

    //by using if condition with cross check
    try{

        if(driver.findElement(By.id("demo")).isDisplayed()==true){

            System.out.println("clicked correctly");
        }

    }catch(Exception e){

        System.out.println("U Clicked Button1 text is not displayed. so failing or throwing again to fail test");
        throw new Exception(e.getMessage());
    }

    //simply by using testng assertion
    //Assert.assertEquals(driver.findElement(By.id("demo")).getText(), "U Clicked Button1");

    //using page source
    String pagesource=driver.getPageSource(); 

    if(pagesource.contains("U Clicked Button1")){

        System.out.println("search done correctly from page source");
    }else{

        System.out.println("not found in page source");
    }

谢谢

您的页面源是否有相同的字符串?对于给定的字符大小写?实际上,当我们尝试使用getPageSource方法时,html标记也会出现,我也很累地删除了它,但由于空格和换行符的原因,某些地方的数据无法匹配。我也删除了它,并尝试使用标记名体和获取文本,但它无法从网页中获取全部数据。我想要一些解决方案,我可以检查数据是否存在,而不必担心数据是否与我的描述非常匹配