0

If I used this code:

@FindBy(how = How.XPATH, using= ".//*[@class='leaflet-control-pan leaflet-control']")
private WebElement movingPageButtonsLocator;

The element movingPageButtonsLocator is found, but if I use following code it doesn't:

@FindBy(how = How.CLASS_NAME, using= "leaflet-control-pan leaflet-control")
private WebElement movingPageButtonsLocator;

Aren't both the same ?

If not, how is How.XPATH different from How.CLASS_NAME in this case ?

1
  • they are not the same, one is XPATH and matches any XPATH query, the other is something else, no where in the documentation does it suggest that they are equal in any way. Commented Sep 7, 2017 at 14:36

3 Answers 3

1

This can be done using CSS Selector

 @FindBy(how = How.CSS, using= ".leaflet-control-pan.leaflet-control")
Sign up to request clarification or add additional context in comments.

Comments

1
@FindBy(how = How.XPATH, using= ".//*[@class='leaflet-control-pan leaflet-control']")
private WebElement movingPageButtonsLocator;

Will match any XPATH query, which can be anything on the page. Where className is a single class on an element if you want to match multiple classes on an element you need to use @FindBys.

According to the JavaDoc, this is how it should be:

@FindBys({@FindBy(how = How.CLASS_NAME, using= "leaflet-control-pan"),
          @FindBy(how = How.CLASS_NAME, using= "leaflet-control") })
private WebElement movingPageButtonsLocator;

or more succinctly:

@FindBys({@FindBy(className = "leaflet-control-pan"),
          @FindBy(className = "leaflet-control") })
private WebElement movingPageButtonsLocator;

@FindBys is a logical AND it will only find where both class are on an element. I think it should have been called @FindByAll to be semantically correct

Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags in a chain as described in ByChained

@FindAll is a logical OR it find where any of the specified criteria match an element. I think it should have been called @FindByAny to be semantically correct

Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags It will then search for all elements that match any of the FindBy criteria. Note that elements are not guaranteed to be in document order.

Comments

0

if you will look at carefully how = How.CLASS_NAME says class name and not class names. see singular vs plural.

So How.CLASS_NAME should be used only when a element could be identified by single class.

Following will work, not sure if it will give you an element you are interested in or not.

@FindBy(how = How.CLASS_NAME, using= "leaflet-control-pan")
private WebElement movingPageButtonsLocator;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.