Accessing elements inside iFrame documents in Selenium WebDriver

Viewed 4008

I am trying to automate a scenario where I would need to access elements inside an iFrame and continue my testing. My current problem is that, I am able to switch to the iframe successfully, but I am unable to access contents inside the frame. The contents inside the iframe are Shadow DOM contents and are inside a document.

The HTML code looks like this::(Please see attachedHTML)

I have tried the below and get org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element

  1. By accessing the shadow root

    driver.switchTo().frame("iframe_id");    
    WebElement root1 = driver.findElement(By.className("class_abs"));          
    WebElement shadowRoot1 = expandRootElement(root1);   
    

    Here webdriver is unable to find element by classname.

2.By using Javascript executor

driver.switchTo().frame("iframe_id");        
JavascriptExecutor js=(JavascriptExecutor) driver;        
js.executeScript("arguments[0].click();",  driver.findElement(By.xpath("button_xpath")));

Here webdriver is unable to access the Xpath.

3. Using normal Xpath after switching to IFrame.

I would like to know if I need to access the contents of the document inside iFrame first to access its elements. If that is the case, how can I do it.

P.S- Tried this on both chrome and Firefox browser.

1 Answers
Related