Manipulating xpath in order to post data to a sheet in Selenium with Java

Viewed 38

I wonder if it is possible to use an index to increase the range of the xpath of a given sheet to fill a given sheet using Java and Selenium


I'm working with a 3 x 3 matrix-sheet and i want to insert data to different columns:

The XPATH of the first box and first column is this:

//*[@id="grid"]/table/tbody/tr[3]/td[3]/input

The XPATH of the second box and first column is this:

 //*[@id="grid"]/table/tbody/tr[4]/td[3]/input

The XPATH of the third box and first column is this:

//*[@id="grid"]/table/tbody/tr[5]/td[3]/input

The XPATH of the fourth box and first column is this:

//*[@id="grid"]/table/tbody/tr[6]/td[3]/input

The XPATH of the fifth box and first column is this:

//*[@id="grid"]/table/tbody/tr[7]/td[3]/input

So, the pattern says that "tr" is the box number of each column "td"

Take a look at this picture (i cannot upload images yet):

image


The third column has these Xpath:

The XPATH of the first box and third column is this:

//*[@id="grid"]/table/tbody/tr[3]/td[5]/input

The XPATH of the second box and third column is this:

//*[@id="grid"]/table/tbody/tr[4]/td[5]/input

The XPATH of the third box and third column is this:

//*[@id="grid"]/table/tbody/tr[4]/td[5]/input

The XPATH of the fourth box and third column is this:

//*[@id="grid"]/table/tbody/tr[4]/td[5]/input

The XPATH of the fifth box and third column is this:

//*[@id="grid"]/table/tbody/tr[4]/td[5]/input

Take a look at this picture:

image


My question is how can i post data from a data-set and paste it in the different boxes? How can i manipulate the xpath?

I mean, i want to paste these values:

[{1,2},{3,4},{90,90},{143,234},{09,89}]

Expected result:

[1  |  |2  ]
[3  |  |4  ]
[90 |  |90 ]
[143|  |243]
[09 |  |89 ]

Is it possible to put an index on the xpath?:

//*[@id="grid"]/table/tbody/tr[i]/td[j]/input

This is how i post data but it is harcoded:

driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[3]/td[3]/input")).sendKeys("1");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[4]/td[3]/input")).sendKeys("3");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[5]/td[3]/input")).sendKeys("90");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[6]/td[3]/input")).sendKeys("143");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[7]/td[3]/input")).sendKeys("09");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[3]/td[5]/input")).sendKeys("2");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[4]/td[3]/input")).sendKeys("4");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[5]/td[5]/input")).sendKeys("90");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[6]/td[3]/input")).sendKeys("243");
driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr[7]/td[5]/input")).sendKeys("89");


How can i post the data by using the collection and indexes on the xpath?
1 Answers

Sounds like you want to do something like this:

int[] rows = new int[]{3, 4, 5, 6, 7, 3, 4, 5, 6, 7};
int[] columns = new int[]{3, 3, 3, 3, 5, 3, 5, 3, 5};
int[] data = new int[]{1, 3, 90, 143, 90, 2, 4, 90, 243, 89};

for (int i = 0; i < data.length; i++) {
    driver.findElement(By.xpath("//*[@id=\"grid\"]/table/tbody/tr["+rows[i]+"]/td["+columns[i]+"]/input")).sendKeys(""+data[i]+"");
}

(Ofcourse this is far from optimal, since everything is still hardcoded.)

Related