使用webdriver查找webelement的多个定位器

问题描述 投票:4回答:4

我正在使用Selenium Webdriver和QAF。我面临的问题与在网页上查找元素有关。对于少数元素,不同的定位器在不同的时间工作。

例如 - 有时name = nameA有效,有时name = nameB(可能取决于AUT的不同环境,我不知道)。

查找以下代码:

public class HomePage extends WebDriverBaseTestPage<WebDriverTestPage> {


    @FindBy(locator="name=nameA")
    private QAFWebElement btnSomeElement;


    @Override
    protected void openPage(PageLocator locator, Object... args) {
        driver.get("/");
    }
}

我该怎么做才能解决这个问题?

java selenium selenium-webdriver testng qaf
4个回答
3
投票

虽然您已经在使用QAF,但您已经拥有适用于此类用例的解决方案。首先你应该使用Locator存储库,而不是在页面中硬编码定位器只提供定位器密钥。

例如:

在page.loc文件中

my.ele.locator=<locatorStretegy>=<locator>
my.ele.locator=name=elemName

在Page类中:

@FindBy(locator = "my.ele.loc")
private QAFWebElement btnSomeElement; Now coming to your problem, if most of the locator very with environment then you can utilize

resource management capabilities of QAF。在其他情况下,您可以使用QAF提供的alternate locator strategy。例如:

my.ele.locator=['css=.cls#eleid','name=eleName','name=eleName2']
my.ele.locator=['name=eleNameEnv1','name=eleNameEnv2']

1
投票

您应该获得一个匹配两个环境的选择器,使用css或xpath选择器。

如果您需要任何帮助,请添加包含sections / elements的html代码段。

假设您按名称选择并且此名称更改,您可以编写一个匹配两个名称的选择器,如:

css:[name=nameA], [name=nameB] xpath://*[@name='nameA' or @name='nameB']


0
投票

定位器有时会发生变化是很常见的。有些甚至是动态的,尤其是表行中的元素。最好使用更具体的定位器(如xpath)来定位元素。

@FindBy(locator = "xpath=//button[text()='ButtonName']")
private QAFWebElement btnSomeElement;

0
投票
btn.login.page.logon.button = {\"locator\":[\"xpath=//div[contains(@id,'logonbutton')]//span[contains(text(),'Logon')]\",\"xpath=//*[contains(text(),'Logon') or @id='__screen0-logonBtn']\"]}

这是在QAF中使用多个定位器的纯文本文件的相同示例。适合我。

© www.soinside.com 2019 - 2024. All rights reserved.