【问题标题】:Button is not visible and returning TRUE, button xpath is in web page,for selenium webdriver按钮不可见并返回 TRUE,按钮 xpath 在网页中,用于 selenium webdriver
【发布时间】:2021-03-09 14:02:56
【问题描述】:

使用 selenium webdriver 测试按钮是否不可见,布尔值始终返回 true。

使用下面的代码并返回 TRUE,在网页中我可以看到 Add 按钮的 xpath。按钮不可见。

    boolean present;  
    try { 
        //driver.findElement(By.xpath("//button[@id='add']")); 
        driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
       present = true;
       System.out.println("Add button is present");   
    } catch (NoSuchElementException e) { 
       present = false;
       System.out.println("Add button is not present:" +e.getMessage()); 
}

xpath: addButtonxpath

【问题讨论】:

    标签: java selenium webdriver


    【解决方案1】:

    这是正确的行为

    driver.findElement(By.xpath("//button[@id='add']")).isDisplayed(); 这将返回为 false,但是您已在下一行明确定义 present = true;。

    如果..else 块你可以做什么

            boolean present;  
            if (driver.findElement(By.xpath("//button[@id='add']")).isDisplayed())
            {
                present=true;
                System.out.println("Add button is present");  
            }
            else
            {
                present = false;
                System.out.println("Add button is Not present");  
            }
    

    【讨论】:

    • 输出仍然:添加按钮存在。按钮本身不在网页中
    【解决方案2】:

    这里是代码sn-p

    boolean present;
    try {
        present = driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
        System.out.println("Add button is present");
    } catch (NoSuchElementException e) {
        present = false;
        System.out.println("Add button is not present:" + e.getMessage());
    } catch (Exception e) {
        present = false;
        System.out.println("Add button is not present:" + e.getMessage());
    }
    

    【讨论】:

    • 仍然出现添加按钮
    • 可能是DOM中存在的元素,DOM中是否存在多个添加按钮,您可以检查可见性:可见;属性,获取可见性属性值并检查其是否隐藏
    • 只有一个按钮,我检查了未标记为隐藏的元素
    【解决方案3】:

    Working of isDisplayed() 方法值得怀疑和争论的话题。话虽如此,在您的情况下isDisplayed() 不起作用,因为它正在获取 DOM 中的元素并返回 true,即使该元素在 UI 上不可见。

    通过检查您是否可以得出结论,此按钮的 DOM 中有样式属性。喜欢-

    driver.findElement(By.xpath("//button[@id='add' and @style='display:none;']")

    现在启用按钮并检查样式属性,它将更改为使用该值的其他内容并使用该值创建 xpath。并检查添加按钮元素的存在。那么您的isDisplayed() 方法可能会按预期工作。

    【讨论】:

    • 谢谢!当我将 xpath 指定为“//button[@id='add' and @style='display:none;']”时它起作用了
    猜你喜欢
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多