How to Handle Iframe in Selenium Webdriver Using Java

How to handle iframe in Selenium WebDriver using java

In Webdriver, you should use driver.switchTo().defaultContent(); to get out of a frame.
You need to get out of all the frames first, then switch into outer frame again.

// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click();

How to click on a link inside an iframe which is inside a frame using Selenium Webdriver and Java

I was able to get get it working by switching content by index. Using System.out.println(driver.getPageSource()); to log page source to console and make sure i am working at the right spot.
I tried to find iframes by tag and print their name as following.

List<WebElement> elements = DriverContext.driver.findElements(By.tagName("iframe"));
elements.forEach(element -> System.out.println(element.getAttribute("name"))); // printed 'menu' and 'body', perfect

But, while switching content to iframe using name, tried also using WebDriverWait,

driver.switchTo().frame(driver.findElement(By.name("menu")));

for whatever reason, it didn't work and kept throwing the exception as in the question.

Using index to switch to iframe followed by click on the element worked.

driver.switchTo().frame(0); // switch to frame
driver.switchTo().frame(0); // switch to first iframe

driver.findElement(By.id("elmt_XXXX_subMenu")).click();

Also, i had used WebDriverWait to wait for page load.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(webDriver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete"));

Is there any solution to bypass the cookie iframe by using selenium webdriver?

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:

  • Ways to deal with #document under iframe
  • Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?

Find elements inside forms and iframe using Java and Selenium WebDriver

Before you try searching for the elements within the iframe you will have to switch Selenium focus to the iframe.

Try this before searching for the elements within the iframe:

driver.switchTo().frame(driver.findElement(By.name("iFrameTitle")));

Navigating nested un-named iframes with selenium webdriver switchTo()

Found an answer to my problem.

Be aware of the fact that each iframe starts at the index 0.

Therefore if you have nested iframes without name or id -- something like this

(Frame C) is nested in (Frame B) which is nested in (Frame A)

  • Frame A
  • ---Frame B
  • ------Frame C

you would try something like this

    driver.switchTo().frame(0)
driver.switchTo().frame(0)
driver.switchTo().frame(0)

in order to get to "Frame C"

Switching to iframes in Selenium-Java

Try to execute this code.

WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
driver.findElement(By.xpath("//a[text()='Enter Country']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='combobox']")));
dropdown.selectByVisibleText("Portugal");
driver.switchTo().defaultContent(); //Move outside to the frame.


Related Topics



Leave a reply



Submit