Unable to send key in IFrame

111 views
Skip to first unread message

rajeev singh

unread,
Mar 4, 2016, 7:33:06 AM3/4/16
to Selenium Users
Hi,

I am trying to send text in the element attached with iframe...but failing each time...have been used below code

@FindBy(xpath="//div[@id='midContainer']/form/table[2]/tbody/tr[11]/td[2]/div/div[2]/iframe")
//@CacheLookup
WebElement  tiframe;
@FindBy(tagName="body")
WebElement text_box;

    
       driver.switchTo().frame(tiframe);
       text_box.click();
       text_box.sendKeys("abc");


screenshot is attached.

































iframe issue.png

Anand Somani

unread,
Mar 4, 2016, 7:51:47 AM3/4/16
to seleniu...@googlegroups.com

Thanks & Regards,
Anand Somani
LinkedIn Profile : https://www.linkedin.com/in/somanianands
Skype ID: anand.somani

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/8fbd2443-0232-41b4-8b4a-b39bf7cd43ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rajeev singh

unread,
Mar 8, 2016, 1:48:53 AM3/8/16
to Selenium Users
Thank you Anand....but still I am unable to enter text in that frame. 

I used below code to find out number of frames and element within it:

List<WebElement> frameList=driver.findElements(By.tagName("iframe"));
System.out.println("no of frames on page: "+ frameList.size());
frameList=driver.findElements(By.tagName("body"));
System.out.println("no of body tag on page is: "+ frameList.size());


     ((JavascriptExecutor)driver).executeScript("window.scrollTo(0," +driver.findElement(By.xpath("//div[@id='midContainer']/form/table[2]/tbody/tr[11]/td[2]/div/div[2]/iframe")).getLocation().y+")");

WebElement descriptionElement = driver.findElement(By.xpath("//div[@id='midContainer']/form/table[2]/tbody/tr[11]/td[2]/div/div[2]/iframe"));

driver.switchTo().frame(descriptionElement);
WebElement editable= driver.switchTo().activeElement();

System.out.println(editable.getTagName());  //////output is body

editable.findElement(By.tagName("body")).sendKeys("abc");
// editable.sendKeys("abc");
driver.switchTo().defaultContent();

O/P:
no of frames on page: 1
no of body tag on page is: 1

org.openqa.selenium.NoSuchElementException: Unable to find element with tag name == body (WARNING: The server did not provide any stacktrace information)

Dinesh Kumar R-"The Developer in Test"

unread,
Mar 8, 2016, 2:21:54 AM3/8/16
to Selenium Users
Hi Rajeev singh-

Try to locate your iframe's editor with different locator rather than tagname('body'). There can be a body tag for your iframe as well as your document also, so there can be ambiguity as per my knowledge.

Regards,
Dinesh Kumar R.

rajeev singh

unread,
Mar 8, 2016, 5:17:01 AM3/8/16
to Selenium Users
Thank you Dinesh...there is only one frame which is created by jquery in MVC ...... i used below approche to send key n it worked. :)

     ((JavascriptExecutor)driver).executeScript("window.scrollTo(0," +driver.findElement(By.xpath("//div[@id='midContainer']/form/table[2]/tbody/tr[11]/td[2]/div/div[2]/iframe")).getLocation().y+")");

WebElement descriptionElement = driver.findElement(By.xpath("//div[@id='midContainer']/form/table[2]/tbody/tr[11]/td[2]/div/div[2]/iframe"));

driver.switchTo().frame(descriptionElement);
WebElement editable= driver.switchTo().activeElement();

System.out.println(editable.getTagName());  //////output is body

editable.findElement(By.tagName("body")).sendKeys("abc");
// editable.sendKeys("abc");

setClipboardData("testdata");
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.delay(2000);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
System.out.println("test description is added");
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//robot.keyPress(KeyEvent.VK_ENTER);
//robot.keyRelease(KeyEvent.VK_ENTER);
driver.switchTo().defaultContent();


public static void  setClipboardData(String str){
StringSelection ss = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
System.out.println("done");
}

Regards,
Rajeev
Reply all
Reply to author
Forward
0 new messages