I want to write my own locator to access the elements. WebDriver’s API offers currently eight locators allowing to retrieve elements by id, name attribute, tag name, complete or partial link text, XPath, class name, and css selector. However those default locators not enough for me now because I have to access the elements through a new attribute. Let me give an xample so that you can understand what I really want here.
Example:
Choose your username:
Now I want to write a code so that I can access the username button using the myLocator locator like:
*driver.findElement(By.myLocator("username")).*
It would be very helpful if anybody can give us some good idea how could I rewrite the BY class to add my own locator.
Thank you in advance for your help.
You would need to subclass the
Byclass and provide an implementation forfindElementandfindElementsmethods, since this is where the ‘meat’ of the actual element finding occurs.You should then be able to use it with the normal
driver.FindElementthen.