7

In Selenium is it possible to identify the object by its type? e.g.

<input type="text" name="FirstName" value="">

Here can I identity the above element by its type of "text"?

1
  • Please do one question at a time. I also added code tagging as the code wasn't showing. Nov 5, 2015 at 13:04

4 Answers 4

5

A slightly different interpretation of the question just in case :)

Lets say you have already located one or more elements in your test and you want to identify them by their 'type' html attribute...

// Lets locate the first found input tag
WebElement someElement = driver.findElement(By.cssSelector("input")); 

// And now lets store it's type
String typeOfElement = someElement.getAttribute("type"); 

Or if you want to deal with a list of elements

// Locate all input tags on the page
List<WebElement> someElements = driver.findElements(By.cssSelector("input")); 

// Now iterate through them and check for our desired match
for (WebElement anElement : someElements) {
    if (anElement.getAttribute("type").equals("text")) {
        // Do something
    }
}
11

Below are the answer to your questions.

In Selenium is it possible to identify the object by its type. Ex; Here do we able to identity above object by its type call text.

Yes, it is possible to identify the objects by its type. As already mentioned by @Michael Durrant, you can use the CssSelector for that.

driver.findElement(By.cssSelector("input[type='text']"));

Then do we able to count how may object were on the page where its type is text. Lets say some time page may contain more than one object which type is text. So I need the count in that case.

Yes, this too is possible using Selenium. You need to use the following code for that.

int i = driver.findElements(By.cssSelector("input[type='text']")).size(); //this will provide the number of elements with mentioned type
System.out.println(i);

Finally do we able to do separate action after identify those objects. Lest say i need to enter difference values into text fields.

This you can do in a multiple ways:

Solution 1: Using the index of the elements. Once you know the number of elements you can set different value of each element using their index position e.g.

driver.findElements(By.cssSelector("input[type='text']")).get(1).sendKeys("Name");

Remember, index of elements will start from '0', so if there are 4 elements on your page; then their indexes will be 0, 1, 2, 3

Solution 2: Store your elements to a Array list and then set value of these list elements. You can use CSV/Excel or any other external file too for setting value of these list elements.

driver.findElements(By.cssSelector("input[type='text']")).toArray();
0
0

You can indentify a page element like that with a css selector

input[type='text']
0
0

Identifying an object by its type attribute in Selenium is definitely possible. You can use the By.cssSelector() method to locate the element with a specific type. Here's an example:

// Find the first input element with type attribute as "text"
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));

// Count the number of input elements with type attribute as "text"
int count = driver.findElements(By.cssSelector("input[type='text']")).size();
System.out.println("Number of input elements with type attribute as 'text': " + count);

// Enter values into each input element with type attribute as "text"
List<WebElement> elements = driver.findElements(By.cssSelector("input[type='text']"));
for (WebElement el : elements) {
    el.sendKeys("some value");
}

In the above example, we first locate the element with a specific type attribute by using the By.cssSelector() method. We can also count the number of elements with the same type attribute by using driver.findElements() method and size() method. Finally, we can enter values into each input element with the same type attribute by using a for loop and sendKeys() method.

I hope this helps! Let me know if you have any further questions.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.