Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<'WebDriver'>.switchTo().frame(<'frameId'>) not working with Chrome driver

Need to access elements inside a modal iframe.

Below code works perfectly fine for FireFox driver while fails for Chrome -

String frameId = null;
List<WebElement> frameSet = driver.findElements(By.tagName("iframe"));  
for (WebElement frameName : frameSet){  
    if(!(frameName.getAttribute("id").isEmpty()) && (frameName.getAttribute("id").contains("DlgFrame"))){
            frameId = frameName.getAttribute("id");
        }
}

try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    }

Logger.info("Switch to Frame - "+frameId);
driver.switchTo().frame(driver.findElement(By.id(frameId)));

Does Chrome driver support switchTo.frame(<'frameId'>)?

Error while using Chrome Driver -

org.openqa.selenium.WebDriverException: Unknown command. Options: ActivateTab, CaptureEntirePage, CloseTab, DeleteCookie, ...

Command duration or timeout: 220 milliseconds Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 22:18:01' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_05' Driver info: driver.version: RemoteWebDriver Session ID: cbde65cb0394ee0434b3bb528918ce40 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244) at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:87) at com.shn.services.Office365.sharePointUploadFile(Office365.java:173) at com.shn.test.RunOffice365Test.testSharePointUploadAndDeleteFile(RunOffice365Test.java:55) at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:74) at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:92) at org.apache.maven.surefire.Surefire.run(Surefire.java:180) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

like image 999
praneel Avatar asked Sep 18 '12 06:09

praneel


Video Answer


2 Answers

I don't know if this will help anyone, but I was having a similar issue polling frames and this is what the solution was for me.

Switch to DefaultContent():

_driver.SwitchTo().DefaultContent();

Get the frames:

IWebElement iFrames = _driver.FindElements(By.XPath("//iframe"));

Poll through the frames, but only switch to it if "Displayed" is true:

foreach (var frame in iFrames)
{
    if (!frame.Displayed)
        continue;

    _driver.SwitchTo().Frame(frame);
}
like image 57
IamBatman Avatar answered Jan 03 '23 14:01

IamBatman


ChromeDriver supports switchTo since it implements the WebDriver Interface. It works fine for me.

You have to do it like this:

driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();
like image 36
Tarken Avatar answered Jan 03 '23 15:01

Tarken