硒元素位置 [英] Selenium Element Location

查看:168
本文介绍了硒元素位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以从另一个元素中找到子元素(这两个项目都是使用PageFactory定位的)?我们有一组包含许多模块的容器,我想确保它们显示在适当的位置。

Is there an easy way to find a child element from another element (both of these items have been located using a PageFactory)? We have a set of containers which hold many modules, and I'd like to ensure that they are displaying in their proper locations.

API似乎只有以下内容方法:

The API only seems to have the following method:

webElement.findElement(s).(By by);

有一种简单的方法可以执行以下操作:

Is there an easy way to do the following:

webElement.findElement(s)(WebElement webElement);

甚至更好:

webElement.contains(WebElement webElement);


推荐答案

我终于得到了你所需要的东西。我最好的解决方案是:

I finally got what you need. My best solution would be something in the way of:

public static void assertContains(WebElement outerElem, WebElement innerElem) {
    // get borders of outer element
    Point outerLoc = outerElem.getLocation();
    Dimension outerDim = outerElem.getSize();
    int outerLeftX = outerLoc.getX();
    int outerRightX = outerLeftX + outerDim.getWidth();
    int outerTopY = outerLoc.getY();
    int outerBottomY = outerTopY + outerDim.getHeight();

    // get borders of inner element
    Point innerLoc = innerElem.getLocation();
    Dimension innerDim = innerElem.getSize();
    int innerLeftX = innerLoc.getX();
    int innerRightX = innerLeftX + innerDim.getWidth();
    int innerTopY = innerLoc.getY();
    int innerBottomY = innerTopY + innerDim.getHeight();

    // assures the inner borders don't cross the outer borders
    final String errorMsg = "ughh, some error message";
    final boolean contains = (outerLeftX <= innerLeftX)
            && (innerRightX <= outerRightX)
            && (outerTopY <= innerTopY)
            && (innerBottomY <= outerBottomY);
    assertTrue(errorMsg, contains);
}

...仅当这些容器都不重叠时才有效。如果他们这样做,我会用 innerElem.getTag() getText()尝试一些黑暗和狂野的魔法测试外部文本是否包含内部元素。一种方法:

...works only if none of those containers overlap. If they do, I'd try some dark and wild magic with innerElem.getTag() and getText() and test whether the outer text contains the inner element. One way of doing it:

public static void assertContains(WebElement outer, WebElement inner) {
    // e.g. //div[text()='some text in inner element']
    final String findInner = ".//" + inner.getTagName() + "[text()='" + inner.getText() + "']";
    try {
        outerElem.findElement(By.xpath(findInner));
    } catch (NoSuchElementException ignored) {
        fail("Some horrible message! We are all doomed!");
    }
    // passed
}

......或者相似的东西。有可能 normalize-space() contains()(或两者)由于标签中的标签而需要... 。

...or something similar. It is possible that normalize-space() or contains() (or both) will be needed due to tags in tags in tags in...

当然,这仍然在猜测。第一种方法可能有误报和否定(但我更喜欢它,考虑到我的需要),第二种方法应该只有误报。你可以自己组合或发明一些东西(例如,如你所说,使用 Reflection )。

And, of course, this is still guessing. The first method may have false positives and negatives (but I like it more given my needs), the second should only have false positives. You can combine them or invent something yourself (e.g. using Reflection as you have already stated).

提交错误

这篇关于硒元素位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆