1

In my current framework (Cucumber, Selenium webdriver, PageObject), we are declaring elements as below

CreateProfile.java //file name

public class CreateProfile {
    private static final By FIRST_NAME = By.id("firstNmae");
    private static final By LAST_NAME = By.id("lastNmae");
    private static final By CLICK_SUBMIT = By.xpath(".//span[@title='{submit}']");
}

followed by methods to enter and click elements.

I want to pass more than one element reference like this

private static final By CLICK_SUBMIT = By.xpath(".//span[@title='{form_submit}']") || By.id("submit") ;

What to do if I want to pass more than one element reference for same element as above?

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Raj
  • 21
  • 3

4 Answers4

1

You can use following css selector.which will identify the element

private static final By CLICK_SUBMIT = By.cssSelector("span[title='{form_submit}'],#submit");
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

You can use 'OR' logic using pipe (|) in Xpath:

private static final By CLICK_SUBMIT = By.xpath("//(span[@title='{form_submit}'] | *[@id='submit'])") 

And if you are using FindBy you might want to see this answer as well.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
1

Selenium provides ByAll implemaneation of By for such cases. Say you have a class with Bys:

class MyBys {
    public static final By CLICK_SUBMIT_BY_TITLE = By.xpath(".//span[@title='{submit}']");
    public static final By CLICK_SUBMIT_BY_ID = By.id("submit");
}

Then you can do something like this:

By mySuperBy = new ByAll(MyBys.CLICK_SUBMIT_BY_TITLE, MyBys.CLICK_SUBMIT_BY_ID);
driver.findElement(mySuperBy).click();
Alexey R.
  • 8,057
  • 2
  • 11
  • 27
0

What you are looking for is available with QAF as alternate locator strategy. You can refer similar question here. The code will look like below:

//if my_ele_loc in properties file
driver.findElement("my_ele_loc").click();

//if my_ele_loc is declared in java
driver.findElement(my_ele_loc).click();

my_ele_loc can either in locator repository out side code or in java code. It will look like:

#out side code in property file
my_ele_loc=['id=submit','xpath=.//span[@title=\'{form_submit}\'']

//java code
public static final String my_ele_loc="['id=submit','xpath=.//span[@title=\'{form_submit}\'']"
user861594
  • 5,733
  • 3
  • 29
  • 45