1

This is the code I have written.

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

        driver.switchTo().frame("content");

        driver.switchTo().frame("detailsDisplay");
        driver.findElement(By.xpath("//a[@title='Part Details']")).click();


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

}

This is the stack trace:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no 
such element: Unable to locate element: 
{"method":"xpath","selector":"//a[@title='Part Details']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 70.04 seconds
For documentation on this error, please visit: 
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '5234b32', time: '2017-03-10 
09:00:17 -0800'
System info: host: 'TMIC-PCS2200', ip: '3.235.34.200', os.name: 'Windows 7', 
os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, 
mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome= 
{chromedriverVersion=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb), 
userDataDir=C:\Users\KT0047~1\AppData\Local\Temp\scoped_dir14184_21633}, 
takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, 
handlesAlerts=true, hasTouchScreen=false, version=66.0.3359.181, 
platform=XP, browserConnectionEnabled=false, nativeEvents=true, 
acceptSslCerts=false, acceptInsecureCerts=false, 
locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, 
takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, 
setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 818a330c1e7b24086d7d06935e830e51
*** Element info: {Using=xpath, value=//a[@title='Part Details']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)

I have attached screenshot of the html code.Html code screenshot

In this code I am trying to click on the 'Part Details' link which is inside 'detailsDisplay' frame and this frame is inside another frame 'content'. I am unable to locate the 'Part Details' link element inside 'detailsDisplay' frame.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20
kiran kumar
  • 35
  • 3
  • 7
  • comment this line( driver.switchTo().frame("detailsDisplay"); ) and try – Pradeep hebbar May 28 '18 at 07:31
  • Tried. Same error. – kiran kumar May 28 '18 at 07:41
  • @kirankumar Please don't ask the same question again and again. Instead work with the answers/solutions provided in the initial version and report back to the answer providers with the status update. – undetected Selenium May 28 '18 at 10:14
  • @DebanjanB. I haven't thought of frames while posting my previous question. Now I am quite sure issue is because of frames. So I don't want to proceed with my previous question and raised a new question. This will sort of remove the confusion to the people. Let me follow up with this. I wouldn't be posting any new question after this. – kiran kumar May 28 '18 at 10:35

3 Answers3

1

You can wait for the frame to fully loaded as given below. It may solve your problem.

 WebDriverWait wait = new WebDriverWait(driver,60);
  wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("content");
  wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("detailsDisplay");
  driver.findElement(By.xpath("//a[@title='Part Details']")).click();
Murthi
  • 5,299
  • 1
  • 10
  • 15
0

As I see there is an iframe element at the top of page that bounds the element you want to interact with. Maybe you can try to locate "Part Details" link like this:

driver.switchTo().frame(driver.findElement(By.xpath("//a[@title='Part Details']")));

EDIT:

It seems that you are still at outside frame, that's why selenium always throw NoElementException. I have an an idea, maybe you can try to find out if iframe actually contains that element.

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

We use webDriver.findElements to avoid NoElementException, if webDriver can find element, then total would be 1, otherwise 0. If this works, then you can proceed further. I hope it helps :)

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
  • I am facing this error.Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@title='Part Details']"} – kiran kumar May 28 '18 at 09:37
  • Have you tried different Locator? For example: `webDriver.findElement(By.cssSelector("a[title*='Part Details']"))` – Ragnarsson May 28 '18 at 09:52
  • Tried. Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"a[title*='Part Details']"} – kiran kumar May 28 '18 at 10:48
  • I found a similar question: https://stackoverflow.com/questions/9130871/finding-nested-iframe-using-selenium-2. Have a look at accepted answer, it might give you an idea. – Ragnarsson May 28 '18 at 11:09
  • In my code driver is successfully switched to 'detailsDisplay' frame as there is no error. But it is unable to find the element inside the frame. – kiran kumar May 28 '18 at 12:12
  • **WebElement contentFrame = driver.findElement(By.id("content")); driver.switchTo().frame(contentFrame); WebElement detailsFrame = driver.findElement(By.id("detailsDisplay")); driver.switchTo().frame(detailsFrame); driver.findElement(By.xpath("//a[@title='Part Details']")).click()**. I added these lines of code but I am facing this error.** Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"detailsDisplay"}** – kiran kumar May 28 '18 at 13:03
  • @kirankumar I have an idea, I just updated my answer. I hope it works :) – Ragnarsson May 28 '18 at 13:47
  • i have tried what you have told me. Output is 11 zeros printed in each new line. No one's. – kiran kumar May 28 '18 at 14:33
0

When you ask a question like this, don't insert the screenshot of the html code. Insert the source. It's easier, for whoever want to answer, try to help.

After the switch to the iframe "detailsDisplay", try to locate your element with:

driver.findElement(By.xpath("//div[@id='tvcTabs0']/div[@class='tabContainer']/ul/li/div[@class='tabcontent]/a[@title='Part Details']")).click();

I didn't test it because I've created this using the screenshot. If you had inserted the source, I could have done a test.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20
  • I am facing this error. Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //div[@id='tvcTabs0']/div[@class='tabContainer']/ul/li/div[@class='tabcontent]/a[@title='Part Details'] because of the following error: – kiran kumar May 28 '18 at 09:47