Waruna Gunaratne
Waruna Gunaratne

Reputation: 81

Using selenium java need to retrieve data from angularjs

I need to retrieve data from the DB using angularjs to selenium java.

Below steps I tried but I couldn't retrieve data.

Use Selenium code---

HTML page source

`<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>`

Selenium Java code

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());`

Upvotes: 2

Views: 2301

Answers (2)

Dao Minh Dam
Dao Minh Dam

Reputation: 373

You can used a common function as:

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

In test script:

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

The same way for getText of an element.

Upvotes: 0

Breaks Software
Breaks Software

Reputation: 1761

Your code is attempting to get the text inside an "input" element, which will return nothing. Please try

cityField.getAttribute("value")

Upvotes: 5

Related Questions