2015-03-02 119 views
-2

我如何獲得項目 - mar3已被授予另一位遊客。下面提供的html文字?如何獲取此元素的文本

我的腳本。

package garbage; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class mailinator { 
static WebDriver driver; 

    public static void main(String args[]) 
    { 
     driver=new FirefoxDriver(); 
     driver.get("http://www.mailinator.com/inbox.jsp?to=679jcscqgtm1guuq09aef3mofu"); 
     driver.findElement(By.xpath("//*[normalize-space(text())='Item awarded']")).click(); 
     // driver.switchTo().frame("rendermail"); 
      System.out.println(driver.findElement(By.xpath("html/body/div[1]/div/div/table[1]/tbody/tr/td[2]/div/table/tbody/tr/td/p[3]")).getText().trim()); 


     } 
} 

而且,HTML

<p> 
Item - 
<b>mar3</b> 
has been awarded to another traveler. 
</p> 

而且,還像here

+1

請提供'html'以及您嘗試過的代碼。 – Saifur 2015-03-02 20:10:52

+1

請避免提供圖片鏈接並將相關HTML作爲文本發佈到問題中。 – alecxe 2015-03-02 20:11:11

+0

問題更新 – 2015-03-02 20:15:03

回答

1

我編輯您的文章。我的理解是你想要該項目成功授權文本。如果是這樣 嘗試

//ul[@id="mailcontainer"]/li[1]/a/div[2] 

我也認爲,按你的評論,內容是iframe內。所以你必須使用

driver.switchTo().frame(driver.findElement(By.cssSelector("[name='rendermail']"))); 

String text = driver.findElement(By.cssSelector("p:nth-child(3)")).getText(); 

System.out.println(text); 

剛剛測試。上面的代碼完美地工作。 enter image description here enter image description here

+0

嗨Saifur, 第一郵件項目一旦你點擊物品獎勵,你會看到一個頁面,它被寫入 **物品 - mar3已被授予另一位旅客。**。 – 2015-03-02 20:30:40

+0

@SusheelThakur查看我的編輯 – Saifur 2015-03-02 20:36:20

+0

不工作得到這個異常 線程「main」中的異常org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「css selector」,「selector」:「p:nnth (3)「} 其不在框架中我在代碼中評論它 – 2015-03-02 20:44:19