0

I am automating a website in which I want to click on the x when a subscription popup comes up The HTML for the X is

<button title="Close popup message" data-testid="email-popup-close-button" id="email-popup-close-button" class="dw-1hfj58t-CloseBtn--EmailContainer e1lwf35p0">×</button>

So to click on this I am using the below

await driver.findElement(By.id("email-popup-close-button")).click();

But it is coming up with the below error

NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[id="email-popup-close-button"]"}

Screenshot attached here

Zach Jensz
  • 3,650
  • 5
  • 15
  • 30

2 Answers2

0

It seems like the issue with locator. Give a try to below.

await driver.findElement(By.xpath("(//button[normalize-space()='×'])[1]")).click();

Or

await driver.findElement(By.xpath("//button[@title='Close popup message']")).click();

Or

WebDriverWait wait = new WebDriverWait(driver,30);
elem = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@title='Close popup message']")));
elem.click();

Import:

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait

Let me know if this does not work.

Akzy
  • 1,817
  • 1
  • 7
  • 19
  • Thank you for your suggestions @akzy I have tried your xpath, full xpath - /html/body/aside[2]/div/div/div[2]/div/div/button, the xpath chrome gives me - //*[@id="email-popup-close-button"], the className, id and same issue I have tried the second answer and it says wait is no defined - unsure how to define this (I am very new to JS) Before this pop up there is another to accept cookies, which i have selected using className, so unsure why this is different and not working – Hannah Miller May 18 '22 at 13:16
  • Have checked the above ? – Akzy May 18 '22 at 13:18
  • I have added the import too. – Akzy May 18 '22 at 13:25
  • @Hannah - I have checked this using this URL https://www.dunelm.com/ and the solution with wait is working for me. I have added all the required import as well. Try the last solution – Akzy May 18 '22 at 13:37
  • Really appreciate the help, when putting the different imports in, it says can only be used in typescript files, do you know the javascript equivalent please? – Hannah Miller May 18 '22 at 13:41
  • Can you share the code and error, actually I tested all this with Java – Akzy May 18 '22 at 14:01
  • When i add the imports in like the above, it says 'import..=; can only be used in TypeScript files – Hannah Miller May 18 '22 at 15:28
  • Can you check this to disable it. https://stackoverflow.com/a/68441308/11863448 – Akzy May 19 '22 at 06:38
0

Found the answer Declared this const {Builder, By, Key, until} = require("selenium-webdriver");

and used

let query = driver.wait(until.elementLocated(By.id('email-popup-close-button')));
    await query.click();