I'm encountering an issue with a CSS locator. The parent has a unique label, which allows me to target the specific child element that I need.
@FindBy(css = "[data-qa='select-Seller'] .select__value-container")
Webelement seller;
public Webelement getSeller(){ return seller; }
Since the class is consistent across all dropdowns, the only variable part is the data-qa
value. Sometimes, I also need to deselect them using an X
locator:
@FindBy(css = "[data-qa='select-Seller'] [data-qa='icon-x']").
As you can see, the initial part remains constant.
My question is whether it's possible to create a method or some other approach that can dynamically update the final segment of the locator. With over 600 dropdowns to manage, modifying each one individually for deselection would be extremely time-consuming.
In my opinion, the ideal solution would involve utilizing default elements (such as .select__value-container
) for actions like element.click/sendkeys/...
, while still being able to change the locator when using something like element.deselect
.
I attempted the following approach:
public void clearDropdown (WebElement element){
String selector = element.toString();
selector = selector.split(" ")[8];
driver.findElement(By.cssSelector(selector + " [data-qa='icon-x']")).click();
}
[[ChromeDriver: chrome on MAC (99c7e4e38147c9f61da0c83c5ef1b992)] -> css selector: [data-qa='select-Seller'] .select__value-container] - this explains the use of "split(" ")[8]".
However, I am unsure if this is the most effective way to address the problem.
Any suggestions you have would be greatly appreciated.