By Locator Mistakes
Please see below a few locators used to find web elements:
By loginLabel = By.xpath("//*[@id=\"loginlabel\"]");
By logoutLinkBy = By.xpath("//*[@id=\"login_drop_panel\"]/div/ul/li[3]/a");
By cardBy = By.xpath("//*[@id=\"root\"]/form/div/span[2]/div[2]/span/input");
How many issues can you find?
Let’s take the locators one by one.
By loginLabel = By.xpath("//*[@id=\"loginlabel\"]");
Since this is a locator and not a label, By should be added to the variable name.
the element has an id attribute with a static and unique value so By.id should be used instead of By.xpath
The correct code should be
By loginLabelBy = By.id("loginlabel");
By logoutLinkBy = By.xpath("//*[@id=\"login_drop_panel\"]/div/ul/li[3]/a");
The double quotes inside of the xpath expression need to be prefixed by \ which makes the expression hard to read; double quotes should be changed to single quotes
* should be replaced with the element tag which is always present and not changing
The correct code should be
By logoutLinkBy = By.xpath("//button[@id='login_drop_panel']/div/ul/li[3]/a");
By cardBy = By.xpath("//*[@id=\"root\"]/form/div/span[2]/div[2]/span/input");
the only thing to do here is to talk to the development team about improving the html code; anything else is a bad idea
Thanks for reading.