I've been attempting to click a button in a pop-up with selenium, but I'm getting the following error:
object [HTMLDivElement] has no size and location
I have tried the normal way, with just click, since it is in the viewport and I am waiting for the page to fully load:
let pressCookieButton = await driver.findElement(By.xpath('//a[@class="btn-actiune btn-actiune--principal"]/..'))
await pressCookieButton.click();
But this does not work. It says
Element not interactible
Then, i tried to click using actions:
let pressCookieButton = await driver.findElement(By.xpath('//a[@class="btn-actiune btn-actiune--principal"]/..'))
const actions = driver.actions({ async: true });
await actions.click(pressCookieButton).perform();
But i get:
object [HTMLDivElement] has no size and location
The HTML that I am attempting to click is the following:
<div class="col-md-4 col-sm-4 col-xs-12">
<a href="javascript:;" onclick="modCookies.updatePermisiuniCookies('toate');" class="btn-actiune btn-actiune--principal">Accept toate cookie-urile</a>
</div>
Below is the full code:
const {Builder} = require('selenium-webdriver');
const {By} = require('selenium-webdriver');
(async function helloSelenium() {
let driver = await new Builder().forBrowser('chrome').build();
try{
await driver.get('https://www.imobiliare.ro/vanzare-apartamente/timisoara/matei-basarab/apartament-de-vanzare-3-camere-XCB61020C');
await driver.sleep(7000);
console.log("clicking")
let pressCookieButton = await driver.findElement(By.xpath('//a[@class="btn-actiune btn-actiune--principal"]/..'))
const actions = driver.actions({ async: true });
await actions.click(pressCookieButton).perform();
//findElement(By.className('btn-actiune btn-actiune--principal'));
await driver.sleep(10000);
await pressCookieButton.click();
}catch(error){
console.log(error)
await driver.sleep(5000);
// await driver.quit()
}
})();
As you can see from the GIF below, the xpath matches two elements and by using driver.findElement you are selecting the first one, while the correct one is the second.
So you should either use driver.findElements instead of driver.findElement and then select the second element, or use a selector targetting only the correct element, such as
driver.findElement(By.cssSelector('div[class*="vizibil-informare"] a[class*="principal"]'))