Mohan
Mohan

Reputation: 63

Accessing elements inside iFrame documents in Selenium WebDriver

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.

Upvotes: 2

Views: 4913

Answers (1)

Mohan
Mohan

Reputation: 63

All,

I have finally managed to resolve the issue. So the issue resolutions goes like this.

-The frame designed on the page were actually hidden. -I had to switch to the hidden frame after switching to the main frame. -Then the elements were then identified easily.

Problems what I faced: 1. In Firefox browser-The hidden frame was not detected in the 'Firefinder' when I checked with filter 'frame' which was detecting all the frames except the hidden frame. Same was the case with 'Firepath' 2. In Chrome browser- Though the document inside the main frame was displayed, traversing inside did not show the hidden iframe.

Solution: In Firefox browser, inspecting the element using the HTML tab helped me to identify the hidden frame inside the main frame. Carefully traversing the iframe hidden document helped me. But, I am still not sure why the Chrome browser 'Inspect' doesn't show this frame.

Upvotes: 2

Related Questions