xpath 识别动态值传递

Pra*_*abu 1 java selenium xpath

xpath

driver.findElement(By.xpath("//input[@accesskey='9']"));
Run Code Online (Sandbox Code Playgroud)

我们如何在 accesskey 属性中传递 Dynamic 值,因为 accesskey 属性分配有任何整数

所以我从数据库中获取该 id 并尝试在 accesskey 属性中传递该 id 如何使用 java?

html代码

td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="6">
</td>
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="7">
</td>
Run Code Online (Sandbox Code Playgroud)

代码

public void portfolioRenewalSearch(String portfolioId) throws Exception {

        try {
            driver.findElement(By.xpath("//input[@accesskey= portfolioId]"))
                    .click();    
        } catch (AssertionError Ae) {
            Ae.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

但上面的代码显示异常“selenium.NoSuchElementException”

Pra*_*ams 5

尝试使用以下代码段在运行时传递值

public void portfolioRenewalSearch(String portfolioId) throws Exception {
    try {
        driver.findElement(By.xpath("//input[@accesskey="+portfolioId+"]"))
                .click();
    } catch (AssertionError Ae) {
        Ae.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)