如何通过硒识别特定的页面对象片段成员元素 [英] How to identify a particular Page Object fragment member element through Selenium

查看:63
本文介绍了如何通过硒识别特定的页面对象片段成员元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们对黄瓜进行了多个硒测试,这些硒测试了特定的成分.这些组件之一是两个标识符,我们将其称为国家/地区标识符和站点标识符.这些具有以下HTML:

In our project we have multiple selenium tests run with cucumber which are testing specific components. One of these components are two identifiers, which we call country identifier and site identifier. These have the following HTML:

<div class="identifiers">   
    <div class="country-identifier">
        <span class="ident-label">
            Germany
        </span>
    </div>
    <div class="site-identifier">
        <span class="ident-label">
            Gaming
        </span>
    </div>
</div>

现在我们的测试有两个模型,每个标识符一个:

Now our tests have two models, one for each identifier:

@PageObject(css = "div.country-identifier")
@Named("CountryIdentifier")
@ScenarioScoped
public class CountryIdentifier {

    @Inject
    @CurrentScope
    private WebElement scope;

    @FindBy(className = "ident-label")
    @CacheLookup
    private WebElement label;

}

并且:

@PageObject(css = "div.site-identifier")
@Named("SiteIdentifier")
@ScenarioScoped
public class SiteIdentifier {

    @Inject
    @CurrentScope
    private WebElement scope;

    @FindBy(className = "ident-label")
    @CacheLookup
    private WebElement label;

}

现在的问题是,当我想访问站点标识符的标签时,WebElement标签的值为德国,而不是游戏.这是因为要获取值的css选择器显然仅应用了类名 ident-label ,而没有考虑容器类.我希望生成的用于查找标签的选择器将为页面对象定义的css与 @FindBy 批注中的选择器结合在一起.有没有办法告诉Web驱动程序仅在 @PageObject 注释的css选择器中指定的容器范围内按类名查找元素?还是我需要 FindBy 批注中的完整CSS选择器,例如:

Now the problem is when I want to access the label of the site identifier the WebElement label is having the value Germany and not Gaming. This is because the css selector to get the value is apparently only applying the class name ident-label without taking into account the container class. I would expect the selector generated to find the label is combining the css defined for the page object with the selector in the @FindBy annotation. Is there a way to tell the web driver to find the element by class name only in the scope of the container specified in the css selector of the @PageObject annotation? Or do I need the full css selector in the FindBy annotation like:

@FindBy(css = "div.site-identifier .ident-label")

还有

@FindBy(css = "div.country-identifier .ident-label")

推荐答案

似乎您快到了.您可以使用以下定位符来标识各个元素:

Seems you were almost there. You can use the following locators to identify the respective elements:

  • 德国:

@FindBy(css = "div.country-identifier span.ident-label")

  • 游戏:

    @FindBy(css = "div.site-identifier span.ident-label")
    

  • 根据您询问有关的评论更新时,可以仅指定".ident-label"并使驱动程序仅在由页面对象注释的css指定的容器内进行搜索,答案是.这是因为 class ident-label 出现在两个< div> 标签下,如下所示:

    As per your comment update as you asked about it is possible to only specify ".ident-label" and make the driver search only inside the container specified by the css of page object annotation, the answer is No. That's because the class ident-label is present under both the <div> tags as follows:

    • 德国:

    <span class="ident-label">
        Germany
    </span>
    

  • 游戏:

    <span class="ident-label">
         Gaming
    </span>
    

  • 因此, WebDriver 实例无法区分它们,并且将始终根据流行的

    So, WebDriver instance can't distinguish them and will always pick up the first match as per the prevailing HTML DOM.

    可以有以下两种解决方案:

    There can be two solution as follows:

    • 最简单的方法是使用父< div> 标记的帮助,该标记的每个 child 标记具有不同的 class 属性.
    • 否则使用 index .
    • The easiest of all take help of the parent <div> tag which have different class attribute for each of it's child tag.
    • Else use index.

    第一种方法是最好的方法,其次是

    The first approach is the best approach and followed mostly.

    这篇关于如何通过硒识别特定的页面对象片段成员元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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