0
    //extract the link texts of each link element

    for (WebElement Page3 : linkElements3) 
    {
        linkTitles3[k] = Page3.getText();
        k++;
    }

    //test each link
    for (String t : linkTitles3) 
    {
            // Titles Click 
            driver.findElement(By.linkText(t)).click();
            System.out.println("\n"+ driver.getTitle());

            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

            if(driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/div/ul[1]/li[3]/a")).isDisplayed())
            {
                driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/div/ul[1]/li[3]/a")).click();     
                driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
                Thread.sleep(4000);  

                System.out.println(driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/dl/dd[3]/a")).getText());
                System.out.println(driver.getCurrentUrl());
                driver.navigate().back();
                driver.navigate().back();
            }
            else
            {
                System.out.println("No Teaching Notes Present");
                driver.navigate().back();

            }
    }

ERROR : Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/div/ul[1]/li[3]/a"}

3
  • Friends, if condition working fine but Else throwing error that xpath in if condition is unable to locate Commented Nov 11, 2016 at 9:38
  • id seems dynamic. never use such dynamic ID to identify an element. the error says the locator (xpath) used, is not able to find the element. so try other locators. share the HTML part which corresponds to the element that you want to identify Commented Nov 11, 2016 at 10:12
  • Hi Naveen, I try all possible locators, its displaying same error. But now it is resolved just if/else wrap placed inside try catch block. Commented Nov 11, 2016 at 11:09

1 Answer 1

1

Wrap the if/else part inside try catch block. Because selenium will through exception if no element with given locator is available in the page.

try{
if(driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/div/ul[1]/li[3]/a")).isDisplayed())
        {
            driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/div/ul[1]/li[3]/a")).click();     
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            Thread.sleep(4000);  

            System.out.println(driver.findElement(By.xpath(".//*[@id='d9c1cb30-3459-4246-919d-41c5fe23de2f']/div/div/article/dl/dd[3]/a")).getText());
            System.out.println(driver.getCurrentUrl());
            driver.navigate().back();
            driver.navigate().back();
        }
        else
        {
            System.out.println("No Teaching Notes Present");
            driver.navigate().back();

        }
}catch(Exception e){
  System.out.println("No Teaching Notes Present");
            driver.navigate().back();
 }

otherwise you can also use isPresent() instead of isDisplayed()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.