B.M.M.
B.M.M.

Reputation: 21

Unable to find elements with multiple classes assigned

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 ?

Upvotes: 0

Views: 368

Answers (3)

user177800
user177800

Reputation:

@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.

Upvotes: 1

Zach
Zach

Reputation: 1006

This can be done using CSS Selector

 @FindBy(how = How.CSS, using= ".leaflet-control-pan.leaflet-control")

Upvotes: 1

Gaurang Shah
Gaurang Shah

Reputation: 12920

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;

Upvotes: 0

Related Questions