1

Im tring to find elements on iframe. But I'm unable to. Here is my code and error I'm getting.

Here is my Script:

public class Add_Lists {

    public static void main (String []args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get("http://automation.cloudaccess.host/administrator"); 

        driver.findElement(By.id("mod-login-username")).sendKeys("admin");

        driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");

        driver.findElement(By.id("mod-login-password")).submit();

        driver.findElement(By.linkText("Content")).click();

        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.linkText("Articles"))).build().perform();

        driver.findElement(By.linkText("Add New Article")).click();

        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

        driver.findElement(By.linkText("Article")).click();

        driver.switchTo().parentFrame();

        driver.siwtchTo.frame(0);

        Thread.sleep(5000);

        WebElement filter = driver.findElement(By.id("filter_search"));

        filter.click();

        filter.sendKeys(Constants.course_Title);
    }

}

I have tried adding waits as well but it didn't help. The Iframe opens up post clicking a button and I wish to interact with iframe elements. But when I try to do so, Im getting the below error:

Error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"filter_search"}
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.15.0-24-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.40.565383 (76257d1ab79276..., userDataDir: /tmp/.org.chromium.Chromium...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 66.0.3359.181, webStorageEnabled: true}
Session ID: cd31b3f4a11905c1be0be001fb3154c4
*** Element info: {Using=id, value=filter_search}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:363)
    at org.openqa.selenium.By$ById.findElement(By.java:188)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
    at testScripts.Add_Lists.main(Add_Lists.java:49)

Please suggest where its going wrong or how can I get this working. Thanks in advance.

  • I can see where you're switching to parent frame, but I cant see where you're switching to the iframe – Alexey R. Jul 31 '18 at 11:06
  • @AlexeyR.: I have updated my script. Please have a look. – Kaustubh Jul 31 '18 at 11:12
  • In your code there is a spelling mistake like you have mentioned as driver.siwtchTo.frame(0); Please correct it to driver.switchTo().frame(0); and switch to the appropriate frame for locating the element. – shanila Jul 31 '18 at 12:06
0

You need to switch into correct iFrame. use below code :

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'administrator/index.php')]")));

And just add some pause before that popup get appears. Replace below code in your code and try

driver.findElement(By.linkText("Article")).click();

Thread.sleep(3000);
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'administrator/index.php')]")));
WebElement filter = driver.findElement(By.id("filter_search"));
filter.click();
filter.sendKeys(Constants.course_Title);

Its working fine I've checked at my end.

  • Thanks! But I'm getting this error: "Expected condition failed: waiting for frame to be available: By.xpath: //iframe[contains(@src,'administrator/index.php')] (tried for 20 second(s) with 500 milliseconds interval)". Can you help me would this happen? – Kaustubh Jul 31 '18 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.