【问题标题】:Java (Eclipse) - Selenium - xpath - can not identify Web ElementJava (Eclipse) - Selenium - xpath - 无法识别 Web 元素
【发布时间】:2021-10-02 14:39:02
【问题描述】:

我对 Selenium 很陌生。

我尝试构建一个测试来验证网页上是否显示了网页元素(小图标)。

因此,在我的班级顶部,我通过 xpath 定义了 Web 元素。
右键单击元素并单击检查后,我从检查中复制了 Xpath。

@FindBy(xpath = "//*[@id="referrals"]/tbody/tr[2]/td[2]/div/img[2]") 私有WebElement ChainAndTwoArrowsIcon;

然后在同一个类中,我有一个方法来评估我的图标是否出现:

public boolean IconChainWithArrowIsFound() throws InterruptedException {
    return ChainAndTwoArrowsIcon.isDisplayed()
            && ChainAndTwoArrowsIcon.getAttribute("title").toString().contains("Associated, and was successfully sent.");   
}

这是我代码的最后一点,我在同一代码中使用的所有 xpath 引用在所有代码中都运行良好。

但是,对于我描述的这一点,我在运行结束时遇到错误:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="referrals"]/tbody/tr[ 2]/td[2]/div/img[2]"}

请看附图

xPath image

我可以就可能出现的问题提出建议吗? 提前谢谢你。

【问题讨论】:

    标签: java testing xpath


    【解决方案1】:
    isDisplayed() 
    

    如果未找到元素,将导致NoSuchElementException。如果发现它会按预期工作。

    基本上,您永远无法确定它是否有效,因为它直接依赖于元素定位器,这不是最佳实践。

    有两种方法可以处理这种情况

    1. 请改用findElements

    代码:

    @FindBy(xpath = "//*[@id='referrals']/tbody/tr[2]/td[2]/div/img[2]") 
    private List<WebElement> ChainAndTwoArrowsIcon;
    
    public boolean IconChainWithArrowIsFound() throws InterruptedException {
        boolean flag = false;
        List<WebElement> elements  = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElements(ChainAndTwoArrowsIcon));
        if (elements.size()>0) {
            System.out.println("Element is visible");
            ChainAndTwoArrowsIcon.get(0).getAttribute("title").toString().contains("Associated, and was successfully sent."); 
            return flag = true;
        }
        else {
            System.out.println("Element is not visible");
            return flag;
        } 
    }
    

    基本上,如果找到元素,findElements 将返回一个列表,否则我们将有一个空列表。根据列表的大小,我们推导出了上面的 if 和 else。

    1. Use try-catch

    代码:

    @FindBy(xpath = "//*[@id='referrals']/tbody/tr[2]/td[2]/div/img[2]") 
    private WebElement ChainAndTwoArrowsIcon;
    
    public boolean IconChainWithArrowIsFound() throws InterruptedException {
        try {
            return ChainAndTwoArrowsIcon.isDisplayed()
                    && ChainAndTwoArrowsIcon.getAttribute("title").toString().contains("Associated, and was successfully sent."); 
        }
        catch (Exception e) {
            System.out.println("Something went wrong.");
            e.printStackTrace();
            return false;
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复。 findElements 为我工作。不幸的是,它不适用于 WebDriverWait。对于我的 IDE,WebDriverWait 显示为已弃用。但是我找到了解决它的方法。我使用的不是 xPath,而是 CSS 选择器,它结合了标签和属性: select.findElements(By.cssSelector("img[src='../images/linked-sent.png']")).isEmpty()) 和它奏效了。
    • WebDriverWait 不依赖于 IDE,它是 Selenium 的一部分。查看我使用 findElements 的第一个代码。
    • 是的,我知道了。谢谢。
    【解决方案2】:

    请检查页面是否有框架/iframe,并且您要查找的元素是否在框架/iframe 内。如果是这样,您需要先使用driver.switchTo().frame('frameName') 切换到上述框架,然后您应该能够找到您的元素。

    【讨论】:

    • 嗨 Saad,谢谢您的回答。不,该页面没有任何框架。我没有在页面源代码中观察到任何
    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多