1

I have an issue to accept a cookie alert on the below website which is embedded in an iframe:

https://www.hamburg.de/

enter image description here

I've tried many ways to solve the issue with using the driver.switchTo().frame() - method:

  • by using the Id of the iframe sp_message_iframe_234327
  • by inserting an index for the iframe 0
  • by calling the iframe through a WebElement WebElement element = driver.findElement(By.xpath("//body/div[6]/iframe[@src='https://cdn.privacy-mgmt.com/index.html?message_id=234327&consentUUID=b2cb90ea-dfdd-4655-b6b9-89117ff34893&requestUUID=ccb96546-c6b5-44e7-9869-438b32f7ad89&preload_message=true']")); driver.switchTo().frame(element);

Unfortunately none of them are working. I'm always getting the following exception:

org.openqa.selenium.NoSuchFrameException

Does anyone have an idea on this specific example to switch on the iframe properly? I'm working with Selenium WebDriver 3.141.59 on Java and my tests should be executed on Mozilla Firefox (80.0.1) and Chromium (85.0.4183.102). Those two browsers are launched headlessly. Glad for any help.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jaw. M.
  • 139
  • 2
  • 15

1 Answers1

2

To click on Alle akzeptieren within the url https://www.hamburg.de/, as the the desired element is within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the desired elementToBeClickable.

  • You can use either of the following Locator Strategies:

    • Using cssSelector:

      driver.get("https://www.hamburg.de/");
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='sp_message_iframe']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[aria-label='Alle akzeptieren']"))).click();
      
    • Using xpath:

      driver.get("https://www.hamburg.de/");
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@id, 'sp_message_iframe')]")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@aria-label='Alle akzeptieren']"))).click();
      
  • Browser Snapshot:

hamburg_de


Reference

You can find a couple of relevant discussions in:

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