如果有4个具有相同名称和动态x路径的按钮,那么下面如何点击它们会找到按钮的xpath。
dr.findElement(By.xpath("html/body/div[3]/div/div/div/div/section/div[2]/div/div/section/section/div[2]/ul/li[5]/a/div[2]/button")).click();
dr.findElement(By.xpath("html/body/div[3]/div/div/div/div/section/div[2]/div/div/section/section/div[2]/ul/li[4]/a/div[2]/button")).click();
dr.findElement(By.xpath("html/body/div[3]/div/div/div/div/section/div[2]/div/div/section/section/div[2]/ul/li[3]/a/div[2]/button")).click();
答案 0 :(得分:0)
您好,在查看了xpath模式后,您可以像下面一样动态处理它们
// hi in your case you can do it like below
String MyFirstXpath = "html/body/div[3]/div/div/div/div/section/div[2]/div/div/section/section/div[2]/ul/li[";
String MySecondXpath = "]/a/div[2]/button";
Please not you can increase or decrease the value of x and i as per your requirement
int x= 3;
for(int i=0;i<3;i++){
System.out.println("Myxpath is : " + MyFirstXpath + x + MySecondXpath);
// here you can perform the operation of click
dr.findElement(By.xpath(MyFirstXpath +x + MySecondXpath)).click();
x++;
}
希望这有助于你