2

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());`
2
  • What error did you get? Where does it fail? Commented May 23, 2016 at 10:48
  • I'm not getting any output values ('System.out.println("+++-- "+cityField.getText());') Commented May 23, 2016 at 10:56

2 Answers 2

5

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

cityField.getAttribute("value")
Sign up to request clarification or add additional context in comments.

2 Comments

cityField.getAttribute("UserProfiles.LastName");
No, try it just as I've entered it. getAttribute() is getting the html attribute "value" of the web element cityField, which will be what's filled into the input control.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.