简体   繁体   中英

Unable to switch between frames in WebDriver

I am working on contacts creation page in www.salesforce.com developer login account. Anybody can create a free developer login account and access this page.

I am unable to switch between frames in Lookup window. There are two frames, one is search frame with textbox and button to search and below that is results frame for displaying search results with hyperlinks for selection. I am specifying the frames by frame name or id, but webDriver switches to search frame and does the searching but then is not able to locate the results frame with NoSuchFrameException.

If I do not switch to search frame initially but directly switch to results frame, it is indeed successful in the locating the results frame which displays results of some recent searches done by default.

How do I switch from search frame to results frame ? Given below is my code

 public class Acc_Parent 
 {
  WebDriver driver;
  FileInputStream fis;
  XSSFWorkbook wb;
  XSSFSheet sh;

  @Test
  public void createParent() throws IOException, InterruptedException, FileNotFoundException 
  {
   driver = new FirefoxDriver();
   //System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
   //driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

  driver.get("https://test.salesforce.com");
  fis = new FileInputStream("C:/Users/psit/Documents/Login.xlsx");
  wb = new XSSFWorkbook(fis);
  sh = wb.getSheetAt(0);
  driver.findElement(By.xpath(".//*[@id='username']")).sendKeys(sh.getRow(1).getCell(0).getStringCellValue());
  driver.findElement(By.xpath(".//*[@id='password']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
  driver.findElement(By.xpath(".//*[@id='Login']")).click();

  fis = new FileInputStream("C:/Users/psit/Documents/Input.xlsx");
  wb = new XSSFWorkbook(fis);
  sh = wb.getSheet("Parent");

  driver.findElement(By.xpath(".//*[@id='Account_Tab']/a")).click();
  driver.findElement(By.xpath(".//*[@id='hotlist']/table/tbody/tr/td[2]/input")).click();
  driver.findElement(By.xpath(".//*[@id='j_id0:acctFrm:nmsrch']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
  driver.findElement(By.xpath(".//*[@id='j_id0:acctFrm']/div[1]/div[2]/table/tbody/tr/td[2]/input")).click();
  try
  {
      driver.findElement(By.linkText("click here")).click();

      driver.findElement(By.xpath(".//*[@id='parentAcc']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='childAcc']")).sendKeys(sh.getRow(1).getCell(2).getStringCellValue());
      Thread.sleep(2000);

      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:0:actFields']")).sendKeys(sh.getRow(1).getCell(3).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:2:actFields']")).sendKeys(sh.getRow(1).getCell(4).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:4:actFields']")).sendKeys(sh.getRow(1).getCell(5).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:6:actFields']")).sendKeys(sh.getRow(1).getCell(6).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:8:actFields']")).sendKeys(Integer.toString((int)sh.getRow(1).getCell(7).getNumericCellValue()));
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:1:actFields']")).sendKeys(sh.getRow(1).getCell(8).getStringCellValue());
      Thread.sleep(2000);
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:3:actFields']")).sendKeys(sh.getRow(1).getCell(9).getStringCellValue());

      String mainWindow = driver.getWindowHandle();
      driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:5:actFields_lkwgt']/img")).click();
      Thread.sleep(5000);
      Set<String> winhand = driver.getWindowHandles();
        for(String str : winhand)
        {   
            if(!str.equalsIgnoreCase(mainWindow))
            {
                driver.switchTo().window(str);
                break;
            }
        }
        try
        {

            driver.switchTo().frame("searchFrame");
            driver.findElement(By.xpath(".//*[@id='lksrch']")).sendKeys(sh.getRow(1).getCell(10).getStringCellValue());
            driver.findElement(By.xpath(".//*[@id='theForm']/div/div[2]/input[2]")).click();
            Thread.sleep(5000);
            driver.switchTo().frame("resultsFrame");
            driver.findElement(By.linkText(sh.getRow(1).getCell(10).getStringCellValue())).click();
            driver.switchTo().window(mainWindow);

            driver.findElement(By.xpath(".//*[@id='pg:acc:pb:acctAdd:rptAddFields:7:actFields_lkwgt']/img")).click();
            Thread.sleep(5000);
            Set<String> winhandle = driver.getWindowHandles();
            for(String str : winhandle)
            {   
                if(!str.equalsIgnoreCase(mainWindow))
                {
                    driver.switchTo().window(str);
                    break;
                }
            }
            try
            {
                driver.switchTo().frame("searchFrame");
                driver.findElement(By.xpath(".//*[@id='lksrch']")).sendKeys(sh.getRow(1).getCell(11).getStringCellValue());
                driver.findElement(By.xpath(".//*[@id='theForm']/div/div[2]/input[2]")).click();
                Thread.sleep(5000);
                driver.switchTo().frame("resultsFrame");
                driver.findElement(By.linkText(sh.getRow(1).getCell(11).getStringCellValue())).click();
                driver.switchTo().window(mainWindow);
            }
            catch(Exception e)
            {
                e.printStackTrace(); 
                System.out.println();
                driver.close();
                driver.switchTo().window(mainWindow);
                System.out.println("State/Region not Found");
            } 

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println();
            driver.close();
            driver.switchTo().window(mainWindow);           
        } 

        driver.findElement(By.xpath(".//*[@id='pg:acc:pb:j_id34:save']")).click();

Before switching to resultsFrame try to switch to defaultContent or your main frame and then switch to resultsFrame . The thing is webdriver searches the frame in current context. So under searchFrame it will never find the resultsFrame (As both might be under main frame).

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM