2016-01-08 34 views
0

如何檢查頁面源中是否存在大文本。 我使用了包含方法來檢查,但條件變得錯誤,因爲序列不準確。 我想解決如何只檢查數據是否存在沒有打擾序列如何檢查頁面源selenium中是否存在大文本java

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"); } 

} 
+0

您的網頁源具有相同的字符串?與特定的個案? –

+0

實際上,當我們嘗試使用getPageSource方法時,html標籤也出現了,我也厭倦了刪除它,但某處的數據並沒有得到匹配,因爲空格和換行符我也刪除了它,並試圖使用標籤名稱體和獲取文本,但它並沒有得到整體來自網頁的數據我想要一些解決方案,在這裏我可以檢查數據是否存在,如果數據與我的描述非常匹配,則不會打擾 – user3495038

回答

0

它似乎字母表大小寫問題,請嘗試條件:

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

    { 

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

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

    } 

在上面,將所有字符轉換爲小寫的字符串,然後比較。

0

如果你想交叉檢查特定位置的數據,那麼最好使用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"); 
    } 

感謝

相關問題