0

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();

        Thread.sleep(5000);

        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("Test");
    }

}

Error I'm getting:

Exception in thread "main" org.openqa.selenium.TimeoutException: 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)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at testScripts.Add_Lists.main(Add_Lists.java:106)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //iframe[contains(@src,'administrator/index.php')]
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: driver.version: unknown
    at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:517)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:513)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    ... 1 more

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kaustubh
  • 506
  • 4
  • 22
  • What do you want to do after login ? – cruisepandey Jul 31 '18 at 18:38
  • Your code works for me, i have tried multiple times – Andrei Suvorkov Jul 31 '18 at 19:15
  • See also: https://bitbucket.org/djarvis/scripted-selenium/wiki/Commands (scroll down for the frame command). And see the [source code](https://bitbucket.org/djarvis/scripted-selenium/src/e0da6a542b5bdc064ffd4870329c1b689c067bc2/source/com/recipefiddle/testing/TestHarness.java?at=master&fileviewer=file-view-default#TestHarness.java-527). – Dave Jarvis Jul 31 '18 at 20:43

2 Answers2

1

Your code seems near perfect. However, I will suggest two changes as follows:

  • When you lookout for the frame to be available and switch to it change the src attribute as follows:

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'http://automation.cloudaccess.host/administrator/index.php?')]")));
    
  • Once you switch to the frame induce WebDriverWait while you search for the elements within that frame as follows:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='filter_search']"))).sendKeys("Test");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks. I have one more scenario where I have an iframe and inside that there is a button on clicking which another iframe gets open. So show should I go with it, can you please suggest? – Kaustubh Aug 01 '18 at 06:56
  • 1
    I will raise a new question. Thanks! – Kaustubh Aug 01 '18 at 07:08
0

By using https://github.com/nick318/FindElementInFrames you can solve your problem like:

SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.xpath("//input[@id='filter_search']")));
WebElement elem = searchInFrame.getElem().orElseThrow();
elem.sendKeys("test");

So you do not even need to declare any specific iframes

nick318
  • 575
  • 4
  • 18