如何在selenium webdriver中使用java在xpath中使用变量?

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

如何在使用java的selenium webdriver中使用xpath中的变量?

这是我的代码行:

for(int row =1; row<=20; row++) 
{   
    driver.findElement(By.xpath(“//*[text()=’PickReuest’]/table/tbody/tr[+row+]/td[2]”)).click(); 
} 

但我得到错误作为无效的xpath。请帮我解决这个问题。

java selenium xpath selenium-webdriver
2个回答
2
投票

xpath表达式字符串可能应该是

"//*[text()='PickReuest']/table/tbody/tr[" + row + "]/td[2]"

还检查PickReuest是否是你想要的。


0
投票

此外,您可以使用String.format();

for(int row =1; row<=20; row++) 
{   
driver.findElement(By.xpath(String.format("//*[text()='PickReuest']/table/tbody/tr[%s]/td[2]", row))).click(); 
}

如果将x路径存储为常量,这种方式会更好。

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