无法定位数据并将数据发送到按下弹出按钮的框架内的网页元素

时间:2014-05-28 12:50:39

标签: java iframe selenium xpath selenium-webdriver

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

------------------------------------------------------------------------------  
    1st Option= Here i assumed that web element is inside the frame which is inside the popup 
-------------------------------------------------------------------------------

    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("shantanunandan@gmail.com");
    driver.switchTo().defaultContent();
    driver.switchTo().window(old);
    System.out.println("Old window "+driver.getTitle());        

-----------------------------------------------------------------------     
    2nd Option= Here i assumed that web element is inside the frame but in the same webpage
----------------------------------------------------------------------- 
    wb=driver.findElement(By.xpath(".//*[@id='GB_window']/div[2]/iframe[@id='GB_frame1']"));
    driver.switchTo().frame(wb);
        System.out.println("Frame Name ="+driver.getTitle());
        wb.sendKeys("shantanunandan@gmail.com");

    wb=driver.findElement(By.xpath(".//*[@id='j_username']"));
    wb.click();
    wb.sendKeys("shantanunandan@gmail.com");
    Thread.sleep(4000);
    wb=driver.findElement(By.xpath(".//*[@id='ui-accordion-accordion-panel-1']/p"));
    System.out.println(wb.getText());
    Thread.sleep(8000);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);    driver.switchTo().defaultContent();
    System.out.println("Current Page Is "+driver.getTitle());

------------------------------------------------------------------------------
       3rd Option= Here i assume that the button is inside a new pop up window
----------------------------------------------------------------------------------
  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='j_username']"));
    wb.click();
    wb.sendKeys("shantanunandan@gmail.com");
    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

在上述代码的帮助下,我试图将一些数据作为LogIn发送到webelement名称。 到目前为止,我尝试了三种方法来定位signIn按钮,但都失败了,给了我异常NoSuchElement。 当我尝试第一个逻辑[第一个选项]时,我得到异常NoSuchElement异常 当我尝试第二个逻辑[第二个选项]时,我得到异常NoSuchElement异常 当我尝试第三个逻辑[第二个选项]时我得到了异常试图访问其他框架内的元素

请告诉我我在哪里犯了错误以及如何访问按钮并向其发送数据

在pic中,当我点击signIn按钮时,窗口出现,我试图在登录ID [红色圆圈内]发送一些数据。直到现在我检查了多次访问按钮的xpath,它们都是正确的,所以我没有机会给出错误的xpath。

请告诉我我在哪里犯了错误以及如何访问按钮并向其发送数据

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我发现frame内有frame,我试图从外frame访问内部frame元素}。我添加到现有代码中的代码如下:

wb=driver.findElement(By.xpath(".//*[@id='GB_window']/div[2]/iframe[@id='GB_frame1']"));
driver.switchTo().frame(wb);
//This is taking me to outer frame
wb=driver.findElement(By.xpath("//iframe[@id='GB_frame']"));
driver.switchTo().frame(wb);
//This is taking me to inner frame which is inside the outer frame
System.out.println("Frame Name ="+driver.getTitle());
wb=driver.findElement(By.xpath(".//*[@id='j_username']"));
wb.click();
wb.sendKeys("sdfs");