Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
191 views
by Contributing Tester (54 points)
edited by

I writing an automation script using Java and TestNG Framework. I am having difficulty to identify the element which is inside the iframe.

Code:

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

I am using 

driver.switchTo().defaultContent();

to perform the action on the element which is inside iframe.

I am trying to identify the element using XPath. Selenium IDE is could find my element, but when I used the same XPath in script in TestNG it was unable to locate the element.

1 Answer

1 like 0 dislike
by The go-to Tester (143 points)
selected by
 
Best answer

To find the element which is inside the iframe, we should first locate the iframe and switch to it and then find the element.

driver.switchTo().frame(nameFrame);
driver.findElement(locator);

 

If you need to scroll the web page you can use following code:

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_PAGE_DOWN);

robot.keyRelease(KeyEvent.VK_PAGE_DOWN);

 

by Contributing Tester (54 points)
It was really helpful! Thank you Sagar.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...