1

I'm trying to close this email subscription popup by clicking on the X. I tried using xpath to select it but I've been unsuccessful.

Code trial:

driver.findElement(By.xpath("/html/body/div[27]/div/div/div/div/div/div/button/svg/circle")).click();

Element snapshot:

enter image description here

Any help would be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
zjboris
  • 25
  • 2

2 Answers2

0

In your case unique selector is [role=dialog] button[aria-label*=Close]

WebDriverWait wdwait = new WebDriverWait(driver, 10);

driver.get("https://www.marksdailyapple.com/");
wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[role=dialog] button[aria-label*=Close]"))).click();
Yaroslavm
  • 1,762
  • 2
  • 7
  • 15
0

The element X within the email subscription popup is a SVG element.


Solution

To click on the svg element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector:

    driver.get("https://www.marksdailyapple.com/#axzz3HChquQM8");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[aria-label='Close form 3'] > svg > path"))).click();
    
  • Using xpath:

    driver.get("https://hausrat.allianz.de/");
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@aria-label='Close form 3']//*[name()='svg']//*[name()='path']"))).click();
    
  • Browser snapshot:

marksdailyapple

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352