0

Here is the screenshot of the html code HTML code screenshot

Here is the code I have written.

public void createPart() {

    try {

        driver.findElement(By.id("username")).sendKeys("502409373");
        driver.findElement(By.id("password")).sendKeys("Magic14Magic");
        driver.findElement(By.id("submitFrmShared")).click();
        //Thread.sleep(10000);
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
        Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
        dropCountry.selectByVisibleText("India");
        driver.findElement(By.xpath("//button[@class='btn']/label")).click();
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);

        //driver.findElement(By.xpath("//span[@class='ds-coachmark-close']")).click();
        //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        //driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);     
        Thread.sleep(10000);
        String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
        driver.findElement(By.xpath("//li[@class='icon-button add']/span")).click();
        driver.findElement(By.xpath("//div[@id='ENCActions']/a/label")).click();

        driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);       
        String subWindowHandler = null;     
        Set<String> handles = driver.getWindowHandles(); // get all window handles
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()){
            subWindowHandler = iterator.next();
            System.out.println("k1");
        }
        System.out.println(driver.switchTo().window(subWindowHandler).getTitle());

        WebDriverWait wait = new WebDriverWait(driver, 10);

        WebElement element = driver.findElement(By.xpath("//select[@id='Type-Field']//following-sibling::div//div//input"));

        element.click();
        Thread.sleep(3000);
        element.sendKeys(Keys.BACK_SPACE);
        Thread.sleep(3000);

        element.sendKeys("Subassy");
        Thread.sleep(4000);

        driver.findElement(By.xpath("//div[@data-value='Subassy']")).click();

        driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
        driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
        driver.findElement(By.xpath("//a[text()='Done']")).click();

        driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);   

        System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());       
        WebElement frame1 = driver.findElement(By.xpath("//div[@id='pageContentDiv']//iframe"));
        driver.switchTo().frame(frame1);
        WebElement frame2 = driver.findElement(By.xpath("//frame[@name='detailsDisplay']"));
        driver.switchTo().frame(frame2);


        driver.findElement(By.xpath("//div[@class='categoryMenu']//following-sibling::div[2]//div//ul//li//div//a[@title='Part Details']")).click();;

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Whats happening here is, I am clicking on a link called 'Create Part' which results a popup. i am changing the window handle. After filling the fields of 'Create Part' page, I am clicking on done. Popup gets automatically closed and I am returining to parent window handle.Main page is refreshed and shows new page. Now I am trying to click on 'Part Details' link which is inside frame 'detailsDisplay' and this frame is inside another iframe 'content'. I am switching to iframe first and frame next and trying to locate 'PartDetails' link, I am unable to locate the element. Here is the stack trace.:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no 
such element: Unable to locate element: 
{"method":"xpath","selector":"//div[@class='categoryMenu']//following- 
sibling::div[2]//div//ul//li//div//a[@title='Part Details']"}

These are methods I have already tried. 1). Used different xpaths to locate the 'part Details' link 2). Used wait conditions for the element and frames to get loaded. 3). I am able to locate frames and element by xpaths in chrome but in code its not working.

I tried this code someone suggested.

// we find all iframes
        //int size = driver.findElements(By.tagName("iframe")).size();
        // we loop through all iframes to search for Part Details link
        /*System.out.println(size);
        for(int i=0; i<size; i++) {
            driver.switchTo().frame(i);
            int total = driver.findElements(By.cssSelector("a[title*='Part Details']")).size();
            System.out.println(total);
            driver.switchTo().defaultContent();
        }*/

The output of this code is all zeros. I really dont understand why I am not able to locate the element in the code.

Note: I have posted the issue but I didn't get solution for my issue. I am struggling with this issue quite a long time. I badly need a solution for this. I want to reach my issue as many people as possible. So, I am posting this again updated. Dont mark this as duplicate. Please suggest me useful solutions.

  • I appreciate you didn't get a working solution from your previous question, but posting the same question again isn't how stack exchange works. – trashpanda Jun 1 '18 at 9:53
  • I feel bad for you, however remember that your "struggling, the bad need, wanting to reach people, not marking as duplicate" are all your needs and not a reason for other people to help. The biggest reason to help is that you present a simple common problem that others will experience. Currently there is so much detail that this question is more of a "fix my problem" for me even if it doesn't help anyone else. Folks are less motivated by that – Michael Durrant Jun 1 '18 at 10:26
0

Instead of using switch frame, try using a selector that also takes the frame into account. My problem with frames has always been that I am unaware that they exists and/or I don't have them in the selector and though my selector is valid it simply doesn't work without the frame selector.

So I have learned that when there is a frame with an id of "f1" then my selector should be ( I use css when possible btw)

frame#f1 element#some_id

Not the answer you're looking for? Browse other questions tagged or ask your own question.