I am working with Selenium and have an issue by closing several HTML Elements, which have the same “close” button. These elements are overlapping each other. My idea was to loop through them and close one by one, but it seems not to work.
First I get all “close” buttons:
let by = By.className("jsPanel-btn jsPanel-btn-close jsPanel-btn-md");
let panelsCloseBtns = await this.getElements(by); //custom func to get close button by xpath
It returns array of Selenium Elements.
Then I have tried 3 approaches, but it works only with the first element:
Using actions:
// Close buttons one by one
for (let i=0; panelsCloseBtns.length; i++) {
if (i === panelsCloseBtns.length) break;
try{
await this.driver.actions().mouseMove(panelsCloseBtns[i]).click().perform();
}catch(ElementClickInterceptedError) {
await this.driver.actions().mouseMove(panelsCloseBtns[i]).click().perform();
}
}
It clicks the first element and then I get the error of ElementClickInterceptedError
Using executeScript
for (let i=0; panelsCloseBtns.length; i++) {
if (i === panelsCloseBtns.length) break;
await this.driver.executeScript(`arguments[${i}].click();`, panelsCloseBtns[i]);
}
The same result as by actions
The third is just click the button
for (let i=0; panelsCloseBtns.length; i++) {
if (i === panelsCloseBtns.length) break;
panelsCloseBtns[i].click();
}
The same result as by actions
I am not sure, how to solve this issue.