1

I'm trying to search few records in a website. The input data is in an excel sheet and I need to write the search result into another excel sheet. And while executing the script, the screen hangs at some point and it is giving NoSuchElementException. But this is not happening always. I have given enough delay, but still it is not loading the page and when I inspect the page it seems the tag in which the element exists is blank.

Given below is my code...

package TestNG;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

import Automation.Emdeon_Elements;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;

public class Emdeon {
    private static WebDriver driver;
    private static Workbook wb1;
    private static Sheet Claims;
    private static Sheet Credentials;
    private static FileOutputStream fs2;
    private static XSSFWorkbook wb2;
    private static XSSFSheet SearchResults;
    private static String Claim;
    private static String filepath2;
    private static String Username;
    private static String Password;
    private static int ex_row_tr;


@Test(priority=1)
public void emdeonlogin() throws InterruptedException {
    driver.switchTo().frame(driver.findElement(Emdeon_Elements.frame1));
    Username = Credentials.getCell(1,0).getContents();
    Password = Credentials.getCell(1,1).getContents();
    driver.findElement(Emdeon_Elements.username).sendKeys(Username);            //Entering Username
    driver.findElement(Emdeon_Elements.password).sendKeys(Password);            //Entering Password
    driver.findElement(Emdeon_Elements.login).click();      
    System.out.println("Logged In");        
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
}

@Test(priority=2)
public void createexcel() throws InterruptedException, FileNotFoundException {
    ex_row_tr=1;
    Date d= new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy HH-mm-ss");
    filepath2 = "D://Jee/Selenium WorkSpace/Input/EmdeonSearchResults_"+dateFormat.format(d)+".xlsx";
    fs2 = new FileOutputStream(filepath2);
    wb2 = new XSSFWorkbook();
    SearchResults = wb2.createSheet("SR");
}

@Test(priority=3)
public void searchscreen() throws InterruptedException, IOException {
    driver.findElement(Emdeon_Elements.Reporting_Analytics).click();
    driver.findElement(Emdeon_Elements.Claim_Mgmt).click();

    int k;
    for(int i=0;i<Claims.getRows();i++)
    {
        k=i+1;
        System.out.println(k);
        for(int j=0;j<1;j++)
        {
            driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
            driver.findElement(Emdeon_Elements.Payments).click();                                                                                       driver.findElement(Emdeon_Elements.Search_Remits).click();                                                                                    
            driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
        driver.switchTo().frame(driver.findElement(Emdeon_Elements.frame2));
            **Select dropdown = new Select (driver.findElement(Emdeon_Elements.Time_Period));**                           
            dropdown.selectByIndex(0);                                                                                                                                    
            WebElement radioBtn = driver.findElement(Emdeon_Elements.By_Claim);                                   
            radioBtn.click();                                                                                                                                             
            Claim = Claims.getCell(j,i).getContents();
driver.findElement(Emdeon_Elements.Patient_Control).sendKeys(Claim);                                    driver.findElement(Emdeon_Elements.Search).click();                                                     
                System.out.println(k+"- "+Claim);   


        }

        Thread.sleep(3000);
        String pp = driver.findElement(Emdeon_Elements.No_Result).getText();
        String qq = "Your search returns no results."; 

        if (pp.equals(qq))
        {
            System.out.println("No search result for "+Claim);
            XSSFRow row2 = SearchResults.createRow(++ex_row_tr);
            XSSFCell cell1 = row2.createCell(0);
            cell1.setCellValue(Claim);  
            XSSFCell cell2 = row2.createCell(1);
            cell2.setCellValue("Your search returns no results.");  
            ex_row_tr++;
        }
        else
        {
            driver.findElement(Emdeon_Elements.Result_1).click();                                               
            Thread.sleep(3000);
            driver.findElement(Emdeon_Elements.Service_Line_Info).click();                                                                                          
        WebElement baseTable1 = driver.findElement(Emdeon_Elements.Heading_Table);

            int Col_count1 = driver.findElements(Emdeon_Elements.Heading_Table).size();
            XSSFRow row1 = SearchResults.createRow(1);

            for(int th_col=1;th_col<=Col_count1;th_col++)
            {
                WebElement th = baseTable1.findElement(By.xpath("//html/body/div/div[10]/div/table/thead/tr/th["+th_col+"]"));
                String c1 = th.getText();
                XSSFCell cell1 = row1.createCell(th_col);
                cell1.setCellValue(c1); 

                XSSFFont font= wb2.createFont();
                font.setBold(true);
                CellStyle style = null;
                style=wb2.createCellStyle();
                style.setFont(font);
                style.setAlignment(CellStyle.ALIGN_CENTER);
                cell1.setCellStyle(style);
            }

            int row_count1 = driver.findElements(Emdeon_Elements.Data_Table).size();

            for(int tr_col=1;tr_col<=row_count1;tr_col++)
            {
                WebElement baseTable2 = driver.findElement(By.xpath("//html/body/div/div[10]/div/table/tbody/tr["+tr_col+"]/td"));
                int Col_count2 = driver.findElements(By.xpath("//html/body/div/div[10]/div/table/tbody/tr["+tr_col+"]/td")).size();
                XSSFRow row2 = SearchResults.createRow(++ex_row_tr);
                XSSFCell cell1 = row2.createCell(tr_col-1);
                cell1.setCellValue(Claim);  

                for(int td_col=1;td_col<=Col_count2;td_col++)
                {
                    WebElement td = baseTable2.findElement(By.xpath("//html/body/div/div[10]/div/table/tbody/tr["+tr_col+"]/td["+td_col+"]"));
                    String c2 = td.getText();
                    XSSFCell cell2 = row2.createCell(td_col);
                    cell2.setCellValue(c2);                 
                }
            }
            ex_row_tr++;
        }
        System.out.println("Completed searching");
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        driver.switchTo().parentFrame();            
    }
    wb2.write(fs2);
    fs2.close();
    System.out.println("Wrote");
    driver.close();
}

@BeforeMethod
public void beforeMethod() throws BiffException, IOException 
{
    String filepath1 = "D://Jee/Selenium WorkSpace/Input/Emdeon.xls";
    FileInputStream fs1 = new FileInputStream(filepath1);
    wb1 = Workbook.getWorkbook(fs1);
    Credentials = wb1.getSheet("Credentials");
    Claims = wb1.getSheet("ClaimList");
}

@BeforeClass
public void beforeClass() 
{
    System.setProperty("webdriver.chrome.driver","D:/Utilities/Selenium/chromedriver.exe");
    driver = new ChromeDriver();        
    driver.manage().window().maximize();        
    driver.get("https://access.emdeon.com/CIHS/");
    System.out.println("URL loaded");
       }
     }

Getting NoSuchElementException while finding "Emdeon_Elements.Time_Period". While inspecting after getting this exception, <body> tag is blank.

Someone please help me to get rid of this.

  • please share html for that element and code for it, i cant see element html which you want to use. – sameer joshi Dec 19 '17 at 9:46
  • <html><head></head><body style="background-color:white;"> </body></html> – Jee Dec 19 '17 at 9:48
  • If element is not on HTML page, How can code find it? – Sagar007 Dec 19 '17 at 9:52
  • The element in which I'm getting error is "//html/body/div/div/div/form/div/div/div/div/div/div/div[6]/select" and it is defined in another class 'Emdeon_Elements'. While getting the exception, the html looks like <html><head></head><body style="background-color:white;"> </body></html> – Jee Dec 19 '17 at 9:55
  • I understood that it is giving error since the html is blank. But why it is not loading? And this is not happening always. Is there any way to get rid of this issue? – Jee Dec 19 '17 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.