Writing scripted browser script to close popup doesn't work

Please paste the permalink to the page in question below:


If asking about a particular script please copy your script or relevant snippet below:


$browser.findElements($driver.By.js("return document.querySelector('div[id^=\"bx-element-6\"] button[type=\"reset\"]')"))
  if (elements != null)
    $browser.executeScript("document.querySelector('div[id^=\"bx-element-6\"] button[type=\"reset\"]').click()");            
    console.log("popup closed");  


Please share your question/describe your issue below. Include any screenshots that may help us understand your question:

For this site www.4wd.com a popup comes up most of the time.
The attached script is working about 50% of the time but fails the other 50%.
Please assist in creating a script to consistently close the popup when it exists.

Hi @paulr - This is a common question and the post below contains several potential solutions.