Your looping logic is correct, but your code produce this error:
org.openqa.selenium.InvalidElementStateException: invalid element state
You need to click
first before taking the next action on that element to become an editable element, and you must use the Actions
class to interact with it:
driver.get("https://keisan.casio.com/exec/system/13800848854767");
List<WebElement> inputTable = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id='var_a_EXL']//tbody//tr")));
System.out.println(inputTable.size());
List<WebElement> columns;
for (int rows = 0; rows < inputTable.size(); rows++) {
if (rows == 2) {
columns = inputTable.get(rows).findElements(By.tagName("td"));
for (int i = 0; i < columns.size(); i++) {
if (i == 3) {
WebElement target = columns.get(i);
Actions actions = new Actions(driver);
actions.moveToElement(target)
.click(target)
.sendKeys("100")
.build()
.perform();
}
}
}
}
And I also added WebDriverWait
to the above code.
Following import:
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
By the way with this xpath //*[@id='var_a_EXL']//tbody//tr[3]//td[4]
there is a simpler way instead of using looping above:
driver.get("https://keisan.casio.com/exec/system/13800848854767");
WebElement target = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='var_a_EXL']//tbody//tr[3]//td[4]")));
Actions actions = new Actions(driver);
actions.moveToElement(target)
.click(target)
.sendKeys("100")
.build()
.perform();
Or use the following css selector to better locate element:
By.cssSelector("#var_a_EXL tr:nth-child(3) td:nth-child(4)")