5

I'm new to automation testing (I'm using TestNG framework and Java language) I'm having trouble in locating an element which is inside iframe.

WebElement iframeSwitch= driver.findElement(By.id("configFrame"));
driver.switchTo().frame(iframeSwitch); System.out.println("Switched");
driver.findElements(By.xpath("//input[@ng-change='ProcessUpdates(m);']")).clea‌​r();
driver.findElement(By.xpath("//input[@ng-change='ProcessUpdates(m);']")).sendK‌​eys("Arsenal"); 
driver.findElement(By.id("SearchAll")).click(); Thread.sleep(15000);
driver.switchTo().defaultContent(); JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("javascript:window.scrollBy(0,300)");

The element which i need to perform action is in the iframe itself, so do i need to use the driver.switchTo().defaultContent();

I tried to find it using the XPath. When I provided the XPath in Selenium IDE, it found the element, but when I wrote the script in TestNG and gave the same Xpath, it returned an exception of unable to locate the element. I'm new to Java as well as to automation. So if someone can provide some hint, it will be great help!

7
  • WebElement iframeSwitch= driver.findElement(By.id("configFrame")); driver.switchTo().frame(iframeSwitch); System.out.println("Switched"); driver.findElements(By.xpath("//input[@ngchange='ProcessUpdates(m);']")).clear(); I used the following script to access the element and i was successful at least in locating the elements in the WebDriver. But how can I now access the elements which is not inside the iframe but in the same window May 12, 2016 at 10:47
  • You mentioned above Selenium IDE- Where are you writing your script in eclipse or anywhere? May 12, 2016 at 11:38
  • Paste your code, what you did & where you stuck. If possible paste your HTML code. May 12, 2016 at 11:40
  • @Bharat Mane WebElement iframeSwitch= driver.findElement(By.id("configFrame")); driver.switchTo().frame(iframeSwitch); System.out.println("Switched"); driver.findElements(By.xpath("//input[@ng-change='ProcessUpdates(m);']")).clear(); driver.findElement(By.xpath("//input[@ng-change='ProcessUpdates(m);']")).sendKeys("Arsenal"); driver.findElement(By.id("SearchAll")).click(); Thread.sleep(15000); driver.switchTo().defaultContent(); JavascriptExecutor js=(JavascriptExecutor)driver; js.executeScript("javascript:window.scrollBy(0,300)"); May 12, 2016 at 12:46
  • Now the issue is that i cannot scroll vertically down of the page. Can someone tell how to effectively scroll . May 12, 2016 at 12:48

6 Answers 6

1

Scrolling page using cordinates:

JavascriptExecutor b = (JavascriptExecutor)driver;
b.executeScript("scroll(0,400)");

scrolling page to perticular location by using webelement:

    JavascriptExecutor b = (JavascriptExecutor)driver;
    WebElement c = driver.findElement(By.xpath(""));
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    b.executeScript("arguments[0].scrollIntoView(true)",c);

hope this helps!!!

0

A common reason for finding something in the seleniumIDE but not programmatically using webDriver is when there are multiple instances. The IDE selects the first automatically but programmaticaly you need to use [0] at the end of the element, e.g. div[0]

0

In a webpage, there is a "Top" window, which can be considered as a default frame (not conceptually) and there can be zero or more iframes.

While executing the automation test script, the driver always points to the default frame which is "Top". If you want the driver to find the element which is inside an iframe, you need to first locate the iframe and switch to it and then ask the driver to find your web element.

To do this, you can use:

driver.switchTo().frame(nameFrame);

Then try to find your web element using:

driver.findElement(locator);

I hope it helps.

3
  • Yes it do helps in locating the element inside the iframe. But I wanted to know how to scroll the page and find another element in the same iframe which is not in the current view May 13, 2016 at 9:06
  • If you are able to locate the webelement using xpath or css locators then while running the automation script, in most cases, the driver would also be able to find it. I don't think you need to scroll the page for the driver to find the element. Please try without scroll, it should work.
    – user6376
    May 18, 2016 at 13:51
  • The element is correctly located but it is below the current display and the element i'm looking for is a button which i need to click. I tried using JavascriptExecutor js=(JavascriptExecutor)driver; js.executeScript("javascript:window.scrollBy(0,700)"); But it is not able to scroll properly. May 20, 2016 at 11:44
0

I had faced the same issue. The problem with Webdriver is that it only finds those elements which are currently loaded in the Dom.

  1. You can use Javascript for scrolling. It works for me:

    public void scrollingToBottomofAPage() {
        driver.navigate().to(URL);
         ((JavascriptExecutor) driver)
         .executeScript("window.scrollTo(0, document.body.scrollHeight)");
    }
    
    public void scrollingToElementofAPage() {
        driver.navigate().to(URL);
        WebElement element = driver.findElement(By.linkText("Import/Export"));
        ((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);
    }
    
    public void scrollingByCoordinatesofAPage() {
        driver.navigate().to(URL);
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
    }
    
  2. You can use robot class for scrolling:

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    
  3. Or you can use keyboard function as well through Actions class. Although its not a good solution but may be it works.

      Actions act = new Actions();
      act.sendKeys(Keys.TAB,Keys.TAB).build.perform();
    
0

Working with Frames using Selenium WebDriver: This link might be helpful.

Blockquote The element which i need to perform action is in the iframe itself, so do i need to use the driver.switchTo().defaultContent();

To access an element which inside a frame, you need to switch to that frame > perform the required operation > and then switch back to the default contents to access elements outside the frame.

0
0

Before you do your operations inside the iframe, first try to check if any iframe is present or not. you can do something like this before your code to verify:

public void getIframe(final WebDriver driver, final String id) {
    final List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
    for (WebElement iframe : iframes) {
        System.out.println("Frame name :" + iframe.getAttribute("name"));
    }
}

If you don't see those iframes listed then with Shailendra's solution of scrolling and checking for iframes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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