Pawan Juyal
Pawan Juyal

Reputation: 261

How to Locate a WebElement when available attributes are not identifiable by findElement method?

Given below is Element(text box) inspected with Fire Bug.

input class="text-input--underbar ng-pristine ng-valid ng-touched" 
    type="tel" placeholder="Mobile Number" ng-model="credentials.loginName" name=""

What attribute of this element should i use in piece of code given below:

driver.findElement(By.

*Note: All the attributes given above are not being identified by findElement method. I'm not using any testing Framework (Junit4, TestNG etc..)

Upvotes: 0

Views: 69

Answers (3)

Pankaj Kumar
Pankaj Kumar

Reputation: 61

You should use '@' for attribute. Use any of the follwing :

1- driver.findElement(By.xpath("//input[@placeholder='Mobile Number']"));

2- driver.findElement(By.xpath("//input[@ng-model='credentials.loginName']"));

For writing xpaths refer to http://www.w3schools.com/xsl/xpath_axes.asp

Upvotes: 0

Dave
Dave

Reputation: 997

Do any of the element's parents have IDs or classes that make them easily detectable? If so, find those elements then search within them for the child element. For instance, if this was your DOM:

<div class="class1">
  <input class="text-input--underbar ng-pristine ng-valid ng-touched" type="tel" placeholder="Mobile Number" ng-model="credentials.loginName" name="" />
</div>

then I would use driver.findElement(By.className("class1")).findElement(By.tagName("input"));

Upvotes: 0

Florent B.
Florent B.

Reputation: 42518

You could use a CSS selector:

driver.findElement(By.cssSelector("input[ng-model='credentials.loginName']")).sendKeys("1234");

Or:

driver.findElement(By.cssSelector("input[placeholder='Mobile Number']")).sendKeys("1234");

Upvotes: 1

Related Questions