如何使用Selenium和Java在webtable单元格中发送文本。

问题描述 投票:1回答:2

我想把值 "100 "发送到位置上。(row: 2, column 3) 的表,这里。

https:/keisan.casio.comexecsystem13800848854767。

下面这段代码,你能告诉我,是什么问题吗?

代码:我想把值 "100 "发送到Table的位置(行:2,列:3),这里:https:/keisan.com

driver.get("https://keisan.casio.com/exec/system/13800848854767");
List<WebElement> inputTable = driver.findElements(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) {
                columns.get(i).clear();
                columns.get(i).sendKeys("100");
            }
        }
    }
} 
java selenium xpath webdriver webdriverwait
2个回答
2
投票

你的循环逻辑是正确的,但是你的代码产生了这个错误。

org.openqa.selenium.InvalidElementStateException: invalid element state

你需要 click 在对该元素进行下一个操作以使其成为可编辑元素之前,必须先使用 Actions 类来与之交互。

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();
            }
        }
    }
}

而且我还添加了 WebDriverWait 到上述代码中。

下面导入。

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

顺便用这个xpath //*[@id='var_a_EXL']//tbody//tr[3]//td[4] 有一个更简单的方法,而不是使用上面的循环。

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();

或者使用下面的css选择器来更好的定位元素。

By.cssSelector("#var_a_EXL tr:nth-child(3) td:nth-child(4)")

1
投票

你还没有提到你所面临的错误。如果能提到这个错误,将有助于我们构建一个更规范的答案。可能你面临的是 InvalidElementStateException 因为你正试图调用 clear() 关于 <td> 元素。

然而,要发送字符串 100 到你需要诱导的表的位置(行:2,列:z)。WebDriverWait 对于 elementToBeClickable() 您可以使用以下方法 定位策略:

  • 使用 xpath:

    driver.get("https://keisan.casio.com/exec/system/13800848854767");
    new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/span[@id='var_a_EXL_2_3C']"))).click();
    new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/input[@id='var_a_EXL_2_3_input']"))).sendKeys("100");
    
  • 浏览器快照。

function_table


行号和列号为变量

考虑到 rowcolumn 数字作为变量。

  • 代码块:

    driver.get("https://keisan.casio.com/exec/system/13800848854767");
    int row = 2;
    int column = 3;
    new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/span[contains(@id, '"+String.valueOf(row)+"') and contains(@id, '"+String.valueOf(column)+"')]"))).click();
    new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/input[contains(@id, '"+String.valueOf(row)+"') and contains(@id, '"+String.valueOf(column)+"')]"))).sendKeys("100");
    

1
投票

+对@frianH的回答,你的代码不能用的原因是: .clear() 只有当该元素是文本输入元素时才有效。文本输入元素是INPUT和TEXTAREA,而你的情况不是这样的。

这里有一个更简单的方法,使用JSExecutor。

    List<WebElement> inputTable = driver.findElements(By.xpath("//*[@id='var_a_EXL']//tbody//tr"));
    System.out.println(inputTable.size());
    List<WebElement> columns;
    int rowss = 3;
    int columnss = 3;

    for (int rows = 0; rows < inputTable.size(); rows++) {
        if (rows == rowss) {
            columns = inputTable.get(rows).findElements(By.tagName("td"));
            System.out.println(columns.size());
            for (int i = 0; i < columns.size(); i++) {
                if (i == columnss) {
                    JavascriptExecutor js = (JavascriptExecutor) driver;
                    js.executeScript("arguments[0].innerText = '300'", columns.get(i));
                }

            }
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.