Reputation:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Iframe {
public static void main(String[] args) throws Exception{
WebDriver driver=new FirefoxDriver();
WebElement wb;
try{
driver.get("http://www.timesjobs.com/");
driver.manage().window().maximize();
System.out.println("Old window "+driver.getTitle());
String old=driver.getWindowHandle();
driver.findElement(By.xpath("//li[1][@class='bdr-left']/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
for(String newi:driver.getWindowHandles()){
driver.switchTo().window(newi);}
System.out.println("New window "+driver.getTitle());
wb=driver.findElement(By.xpath(".//*[@id='GB_window']/div[2]/iframe"));
driver.switchTo().frame(wb);
wb=driver.findElement(By.xpath(".//*[@id='j_username']"));
wb.click();
wb.sendKeys("[email protected]");
driver.switchTo().defaultContent();
driver.switchTo().window(old);
System.out.println("Old window "+driver.getTitle());
}//try
catch(Exception e){
e.printStackTrace();
driver.close();
//driver.quit();
}//catch
finally{
driver.close();
//driver.quit();
}//finally
}//main
}//class
By the help of above code I am trying to send some data in a webelement
names as LogIn.
Here I am trying to send some value in the login id field but when I am clicking on signin button a popup opens. When I right click on the popup then I got an option which says that THIS FRAME then I come to know that its a frame. I tried to switch to it by using driver.switchTo().frame(wb);
where wb is having the path of the frame. When I run the code I got NOSuchElement exception
for the loginid fails
which means the webdriver
is not able to pass the control to the frame.
How can I solve this?
Upvotes: 2
Views: 2561
Reputation: 9
try the below code, it will work fine..
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("GB_frame1")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("GB_frame")));
driver.findElement(By.id("j_username")).sendKeys("TEST");
Upvotes: 0
Reputation:
Maybe there are two frames a frame inside a frame. Check the html document and if you finds so then change the code as necessary
WebElement webele=driver.findElement(By.xpath("//iframe[@id='GB_frame1']"));
driver.switchTo().frame(webele);
webele=driver.findElement(By.xpath("//iframe[@id='GB_frame']"));
driver.switchTo().frame(weble);
Upvotes: 1
Reputation: 1388
You missed out switching to one more iframe
i.e., GB_frame
where your textbox: username
is present inside this parent iframe
:- GB_frame1
Please add the following to your existing switchTo()
frame
code as below:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='GB_frame1']")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='GB_frame']")));
There is no need to switchTo
windows, as it's only one window. You can also use id
instead of xpath
for locating iframes
Upvotes: 4