2016-05-23 20 views
1

我需要使用angularjs從數據庫中檢索數據到selenium java。使用selenium java需要從angularjs中檢索數據

下面的步驟我嘗試過但我無法檢索數據。

使用Selenium代碼---

HTML頁面的源

`<div class="col-sm-6"> 
<input id="LastName" class="form-control ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" type="text" required="" ng-model="UserProfiles.LastName" name="LastName" placeholder="Please Enter Last Name"/> 
</div>` 

硒Java代碼

WebElement cityField = driver.findElement(By.cssSelector("input[ng-model='UserProfiles.FirstName']")); 

cityField.clear(); 
cityField.sendKeys("Chicago"); 
System.out.println("Print- "+ cityField.getAttribute("input[ng-model='UserProfiles.FirstName")); 

cityField = driver.findElement(By.cssSelector("input[ng-model='UserProfiles.LastName']")); 
System.out.println("+++-- "+cityField.getText());` 
+0

你得到了什麼錯誤?它在哪裏失敗? –

+0

我沒有得到任何輸出值('System.out.println(「+++ - 」+ cityField.getText());') –

回答

3

您的代碼試圖得到一個「裏面輸入文本「元素,它將不會返回任何內容。請嘗試

cityField.getAttribute("value") 
+0

我試過但沒有工作 –

+0

cityField.getAttribute(「UserProfiles.LastName」 ); –

+0

不,請按照我輸入的那樣嘗試。 getAttribute()獲取web元素cityField的html屬性「value」,它將填充到輸入控件中。 –

0

您可以使用一個共同的功能:

public String getAttributeValue(WebDriver driver, String locator, String attribute) { 
    WebElement element = driver.findElement(By.xpath(locator)); 
    return element.getAttribute(attribute); 
} 

在測試腳本:

String idValue = getAttributeValue(driver, "//input[@id='LastName']", "id"); 
String modelValue = getAttributeValue(driver, "//input[@id='LastName']", "ng-model"); 
String nameValue = getAttributeValue(driver, "//input[@id='LastName']", "name"); 

對元件的gettext的方式相同。