How to click an element in previous iteration


I have a graph that displays the prices of different products. Some of these prices are the same so that the data points overlap and cannot be clicked. So I used the filter by products feature in order to click on each of the data point. Here’s my script below:

WebDriver driver = DriverFactory.getWebDriver()

// get a collection of products

def products = driver.findElements(By.xpath(‘/html[1]/body[1]/div[@class=“page-view”]/div[1]/div[@class=“module-wrapper-b hide-sm hide-xs”]/section[@class=“card1 field-b”]/div[1]/div[@class=“field-b layout-align-space-between-center layout-row”]/div[@class=“layout-wrap layout-align-start-center layout-row”]/div[@class=“overlay-box products dropdown open”]/div[@class=“overlay-content dropdown-menu”]/div[@class=“layout-align-start-center layout-row”]/div[@class=“truncate”]’))

//loop to select each product

products.each({ def prod →

However, my problem is that, this just click each product, thus, the graph will still display data of several products. I need to click (deselect) the previous product so that only the product for the current iteration will be selected. Anyone who has an idea how to do this?

If the click works to select, surely another click will deselect?

Please refer to the screenshot below:

filter products.png

i write from head, so errors will be in code

def prevElement
products.each({ def prod -> != null){}prevElement=prod