Prabu
Prabu

Reputation: 3728

How to differentiate an Element if all the element xpath are same

Using selenium web driver to automate a testing, in our app Elements (3 input fields) xpath are same, so I can't able to passing the values in that three fields, please find the html

<div class="form-row">
<div class="label-column">
Pan No
</div>
<div class="ctrl-column">
<input class="input-txt" type="text" maxlength="50" data-bind="value: Pan No,qtipValMessage: Pan No" title="">
</div>
</div>
<div class="form-row">
<div class="label-column"> area code </div>
<div class="ctrl-column">
<input class="input-txt" type="text" maxlength="50" data-bind="value: area code,  qtipValMessage: area code" title="">
</div>
</div>
<div class="form-row">
<div class="label-column"> Serial # </div>
<div class="ctrl-column">
<input class="input-txt" type="text" maxlength="50" data-bind="value: Serial #" title="">
</div>

in that above html have an three input fields (Pan No,area code and Serial #) I want to pass an values thru our java code (selenium web driver)

driver.findElement(By.cssSelector("input.input-txt")).sendKeys(
            "acdpp6042c);

if I execute the above one its fill the pan no field, this was no prob, but rest of the fields not able to passing the values, because both three elements tags are same, Please suggest how will generate an xpath for rest of the fields

from developer tool (chrome) xpath is:

//*[@id="divInfo"]/table/tbody/tr/td/div[2]/table/tbody/tr/td[1]/div/div[2]/div[2]/input

above is common for rest of the fields

Upvotes: 0

Views: 4254

Answers (3)

Louis
Louis

Reputation: 151370

Use findElements (with an "s"). I use the Python bindings of Selenium and rarely write Java code (so the following might contain mistakes) but I'd imagine something like:

List<WebElement> els = driver.findElements(By.cssSelector("input.input-txt"));
els.get(0).sendKeys("acdpp6042c);
els.get(1)... // Do something with the second.
els.get(2)... // Do something with the third.

Upvotes: 0

Amith
Amith

Reputation: 7008

If you prefer cssSelectors, the following will work.

css = input[data-bind*='Pan']

css = input[data-bind*='area code']

css = input[data-bind*='Serial']

The symbol * is same as contains in xpath.

Upvotes: 1

Erki M.
Erki M.

Reputation: 5072

what about

//input[contains(@data-bind, "Serial")]

//input[contains(@data-bind, "area code")]

//input[contains(@data-bind, "Pan No")]

Upvotes: 1

Related Questions