【问题标题】:How assign Value to Xpath to find element by WebDriver or WebElement in java code如何为 Xpath 赋值以在 java 代码中通过 WebDriver 或 WebElement 查找元素
【发布时间】:2016-03-30 04:37:02
【问题描述】:

这是我的 Java 代码

List<?> imageList =driver.findElementsByXPath("//*[@class='android.widget.ImageButton' and @index='0']");

我想将类和索引更改为随机值。 我尝试设置两个变量并替换类和索引值,但它不起作用。

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.

有什么办法可以帮我解决这个问题吗? 非常感谢您的时间和帮助。

【问题讨论】:

    标签: java android selenium xpath


    【解决方案1】:

    这样试试

        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+"']");
    

    谢谢, 壁画

    【讨论】:

    • 非常感谢 Murali,我已经尝试过这种方式,它有效。
    猜你喜欢
    • 2023-03-11
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2011-08-14
    • 2018-08-28
    • 2014-08-19
    • 2021-03-31
    相关资源
    最近更新 更多