2

This is my java code

package com.ej.zob.modules;

import org.openqa.selenium.By;

public class SetExchange {
public void Execute(String CountryName, String value)
{
    LaunchApplication.driver.findElement(By.linkText("SET")).click();
    LaunchApplication.driver.findElement(By.linkText("EXCHANGE RATE")).click();
       //LaunchApplication.driver.findElement(By.id("new_Afghanistan_AFN")).sendKeys(value);
    LaunchApplication.driver.findElement(By.xpath("//input[@maxlength='4']")).sendKeys(value);
    LaunchApplication.driver.findElement(By.xpath("//input[@value='SET']")).click();

}

}

this is my html

<div style="display: table-cell;width:270px" name="cell">
<input id="new_Afghanistan_AFN" type="text" maxlength="4">
<input type="button" value="SET"   onclick="setExchangeRate('Afghanistan','AFN')" name="save">
<div id="msg_Afghanistan_AFN"></div>
</div>

There are more than 100 textbox in my webpage and same number of button in front of textbox.I want to insert value in all the 100 textbox sequentially and similarly click on button.But from the above code I am able to insert the value in single text box and similarly click on single button. so my question is how to insert value into all text box and click on multiple button.Each text box is having different id but if I use findelement(By.id) then the code will be too much longer.

1 Answer 1

3

If all buttons and textboxes are surrounded by the same div, you can search for these like:

List<WebElement> divs = LaunchApplication.driver.findElements(By.name("cell")); 

Note that the findElements returns all elements which match the criteria.

Then you can loop through the results like this:

for(WebElement elem: divs){
    elem.findElement(By.cssSelector("[type=text]")).sendKeys("YOUR KEYS TO SEND");
    elem.findElement(By.cssSelector("[type=button]")).click();
}

this will select first the textfield and then the button of each div with name cell. Maybe you need to check if other divs are called "cell" which are not holding these elements.

If you don't know CssSelectors these will match an Attribute Text. For more info look here http://www.w3schools.com/cssref/css_selectors.asp

Another way would be to read in / or create the list of IDs and then get the elements in a loop.

Does this help you, if not please feel free to comment

Sign up to request clarification or add additional context in comments.

7 Comments

By using above code I am getting an error that "unable to locate element by xpath".
the error tels you that you are trying to locate an element by xpath, in my code example there is no By.xpath is it possible that the error is in another line of your code, or did you write something wrong ?
I also use by.css_selector but same error i am getting.
locate element is the problem.may be i am not selecting the correct element.
xpath and css selector both are throwing an error and also can you tell me the another way. like by id.
|

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.