如何在java代码中通过WebDriver或WebElement为Xpath赋值以查找元素

如何在java代码中通过WebDriver或WebElement为Xpath赋值以查找元素,java,android,selenium,xpath,Java,Android,Selenium,Xpath,这是我的Java代码 List<?> imageList =driver.findElementsByXPath("//*[@class='android.widget.ImageButton' and @index='0']"); 有没有办法帮我解决这个问题?? 非常感谢你的时间和帮助 像这样试试看 String ItemName="android.widget.ImageButton"; String Value="0"; String str="//*

这是我的Java代码

List<?> imageList =driver.findElementsByXPath("//*[@class='android.widget.ImageButton' and @index='0']");
有没有办法帮我解决这个问题?? 非常感谢你的时间和帮助

像这样试试看

    String ItemName="android.widget.ImageButton";
    String Value="0";
    String str="//*[@class='"+ItemName+"' and @index='"+Value+"']"; //use this str in code

   //or you can directly use like below

   List imageList =driver.findElementsByXPath("//*[@class='"+ItemName+"' and @index='"+Value+"']");
谢谢,
穆拉里

非常感谢你,穆拉里,我尝试过这种方法,效果很好。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
    String ItemName="android.widget.ImageButton";
    String Value="0";
    String str="//*[@class='"+ItemName+"' and @index='"+Value+"']"; //use this str in code

   //or you can directly use like below

   List imageList =driver.findElementsByXPath("//*[@class='"+ItemName+"' and @index='"+Value+"']");