Shalin
Shalin

Reputation: 424

Multiple locator for finding webelement with webdriver

I am using Selenium Webdriver with QAF. The issue I am facing is related to finding an element on webpage. for few of elements, different locators work at different times.

For example - sometimes name=nameA works and sometimes name=nameB(may be depending upon different environments of AUT, I have no clue).

Find code below:

public class HomePage extends WebDriverBaseTestPage<WebDriverTestPage> {


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


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

What should I do to come over this issue?

Upvotes: 4

Views: 3747

Answers (4)

Kulin
Kulin

Reputation: 1

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

This is an example of the same for plain text file using multiple locators in QAF. Works for me.

Upvotes: 0

user861594
user861594

Reputation: 5908

While you are already using QAF you already have solutions available for such use case. First of all you should use Locator repository, Instead of hard-coding locator in page just provide locator key.

For example:

In page.loc File

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

In Page class:

@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. In other case you can use alternate locator strategy provided by QAF. For example:

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

Upvotes: 3

Harish Ekambaram
Harish Ekambaram

Reputation: 338

It is very common that the locator changes sometimes. Some are even dynamic especially elements in the table rows. It is better to use more specific locator like xpath to locate the element.

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

Upvotes: 0

lauda
lauda

Reputation: 4173

You should get a selector that matches both environments, use css or xpath selectors.

Add html snippet with the sections/elements if you need any help.

Assuming that you are selecting by name and this name changes, you can write a selector that matches both names like:

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

Upvotes: 1

Related Questions