0

In the webpage I'm testing, there are 3 options in a drop down and on selecting each option, rows in a table gets populated. I'm just trying to locate the table value through xpath and I am getting an the exception. Sharing the code I tried and the page source snippets. Please, help me out!!

   System.out.println("\n Unbranded Sample Packs tests: ");
      Reporter.log("   Unbranded sample packs validation   |");

      WebElement sp=driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[3]/div[1]/div/div/a/button"));

      if(sp.isDisplayed())
      {
          sp.sendKeys(Keys.ENTER);
          Thread.sleep(3000);
          String spurl=driver.getCurrentUrl();
          if(spurl.contains("unbranded-sample-packs"))
          {

              //Business card sample kit:

                WebElement w1=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
                w1.click();
                driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[1]/a/span")).click();

                WebElement p=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));

                if(p.isDisplayed())
                {

                    if(p.getText().equals("£8.00"))
                    {
                        System.out.println("\n Business card sample kit displays the correct page");
                    }
                    else
                    {
                        System.err.println("\n Business card sample kit option displays the wrong page !");
                    }
                }
                else
                {
                    System.err.println("\n Prices are not displayed");
                }

            // Reseller Catalogues:

                WebElement selectorElement = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
                try 
                {
                    WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
                    sE.click();
                } 

                catch (NoSuchElementException e) 
                { 
                    selectorElement.click();
                    WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
                    sE.click();

                }

                try 
                {
                    WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));
                    elementOfInterest.click();
                    System.out.println("\n Reseller catalogues option is clicked");
                } 

                catch (NoSuchElementException e) 
                { 
                    selectorElement.click();
                    WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));
                    elementOfInterest.click();
                    System.out.println("\n Reseller catalogues option is clicked");
                }

            //  Select dropdown = new Select(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"))); dropdown.selectByVisibleText("Reseller Catalogues");

    /*      
         * 
         * JavascriptExecutor executor= (JavascriptExecutor)driver;
                executor.executeScript("document.getElementById('ID').style.display='block';");
                Select select = new Select(driver.findElement(By.id("ID")));
                select.selectByVisibleText("value");
                Thread.sleep(6000);    

        */
                new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));




                if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).isDisplayed())
                {
                    new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));

                    if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).getText().equals("£10.00"))
                    {
                        System.out.println("\n Reseller Catalogues displays the correct page");
                    }
                    else
                    {
                        System.err.println("\n Reseller Catalogues displays the wrong page !");
                    }
                }
                else
                {
                    System.err.println("\n Prices are not displayed");
                }

            //Unbranded sample pack option is selected


                WebElement selectorElement1 = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
                try 
                {
                    WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
                    sE.click();
                } 

                catch (NoSuchElementException e) 
                { 
                    selectorElement1.click();
                    WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
                    sE.click();

                }


                try 
                {
                    WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));
                    elementOfInterest.click();
                    System.out.println("\n Unbranded Sample pack option is clicked");
                } 

                catch (NoSuchElementException e) 
                { 
                    selectorElement1.click();
                    WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));
                    elementOfInterest.click();
                    System.out.println("\n Unbranded Sample pack option is clicked");
                }

                new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
                WebElement p3=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));


                if(p3.isDisplayed())
                {

                    new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
                    WebElement p4=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));

                    if(p4.getText().equals("£12.00"))
                    {
                        System.out.println("\n Unbranded Sample Pack displays the correct page");
                    }
                    else
                    {
                        System.err.println("\n Unbranded sample pack displays the wrong page !");
                    }
                }
                else
                {
                    System.err.println("\n Prices are not displayed");
                }

                System.out.println("\n 'Finished size' dropdowns validated \n");
                Reporter.log("   'Finished size' dropdowns Validation done   |");
          }
          else
          {
              System.err.println("\n Sample Pack button links to a wrong path");
          }

      }
      else
      {
          System.err.println("\n Sample packs button is not available");
      }

      System.out.println("\n Unbranded Sample packs validated \n");
      Reporter.log("   Sample packs Validation done   |");

}

Page Source:

Page source for Dropdown options Page source for table

4
  • Please provide error trace and also line number which you have got error.
    – Sagar007
    Commented Nov 19, 2015 at 10:39
  • at what step are you hitting with the exception. Merely providing the code won't help much. Commented Nov 19, 2015 at 18:08
  • I'm using firefox browser.. Even if I add wait statements before locating the elements I get the same error.. Tried out with WebDriverWait, Thread.sleep.. both didn't work.. Commented Nov 25, 2015 at 9:57
  • Out of those three dropdowns, first option is working.. The second and third are not working and it stops in on Selection of the second dropdown..(Reseller Catalogues).. Not sure about the line number.. Please help.. Commented Nov 25, 2015 at 10:01

2 Answers 2

2

I believe you are getting this error because : You are immediately (before the page load could finish) searching for an element and finding it.

there are 3 options in a dropdown and on selecting each option, rows in a table gets populated.

When dealing with asynchronous pages use proper waits after every anticipated page-load.

Insert an explicit wait after the selecting element from drop down and wait for the new content to load. Search for the element you want only after the wait statements. This will also increase the robustness of your tests significantly.

1
0

If you are using chrome browser it might help if you update the chromedriver.exe to the latest version.

The "stale element" and "no such element" errors was fixed in ChromeDriver 2.19

https://sites.google.com/a/chromium.org/chromedriver/downloads

Which browser does this code run on? FF? Chrome?

4
  • 1
    Shouldn't this be a comment ? Commented Nov 19, 2015 at 3:33
  • I don't have 50 reputation to add it as a comment
    – ilm
    Commented Nov 19, 2015 at 3:34
  • 1
    I tried to leave a comment below the author's post but it said that i don't have score of 50 reputation
    – ilm
    Commented Nov 19, 2015 at 8:08
  • Answer edited with more information
    – ilm
    Commented Nov 19, 2015 at 8:15

Your Answer

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

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