Dinu
Dinu

Reputation: 933

How to enter data in table having columns with same id, class, xpath and css using webdriver?

I have a table for entering name, email id and relationship but all the columns are having same id, same class, same name, xpath and css. So how can I write code to add data in the columns. I meant that all the rows under the column "Name" is having same id, same class, same name, xpath and css. So when I code it, the name is entered in first row for name but it is not getting entered in the second row. The HTML for the First column for Name field is

<input type="text" value="" title="Enter name of the user" name="Name" id="Name" class="required wid220"></pre>

The HTML for the second column for Name field is

<input type="text" value="" title="Enter name of the user" name="Name" id="Name" class="wid220 validEntry">

The HTML for the Third column for Name field is

<input type="text" value="" title="Enter name of the user" name="Name" id="Name" class="wid220 validEntry">

Upvotes: 0

Views: 2399

Answers (2)

Major
Major

Reputation: 455

public static void EnterInTable(int rowNumber, string value)
{
    // We don't care about the column as long as you have one text field here
    string query = String.Format("//table[*some-id*]//td[{0}]//input", rowNumber);
    driver.findElement(:xpath, query).sendKeys = value;
}

Upvotes: 0

Akbar
Akbar

Reputation: 1525

If you can talk to the developers of this application, then you need to strongly protest that this is a poor standard of application development. Coming to your query, you can use the column xpath to distinguish between different inputs...example:-

//table/tbody/tr[1]/td[1]/input for first column;  

//table/tbody/tr[1]/td[2]/input for second and so on.....

tr - represents row, td - represents column

If the table has an id or attribute you can use it like this,

//table[@id='value']/tbody/.....

Upvotes: 2

Related Questions