0

I am using selenium webdriver and headless browser for running my script. I am trying to upload file for that element required to use is invisible in html source page. I tried making it visible by using javascript and I can able to make it visible and upload file but it is working on normal fire fox browser. But when I am executing same script on headless browser it is not working below is the html source and code I am using.

<input id="CASE_file_select" class="required" type="file" multiple="" size="25" name="CASE_file" style="display: none;"/>

Code which I am using

    JavascriptExecutor executor = (JavascriptExecutor)oDriver;
    executor.executeScript("document.getElementById('CASE_file_select').style.display='block';");

    oGeneric.sleepTimer(5);
    Utility.constant.objLogs.writeLog("After executing java script");


    WebElement ofilepathxpath = oDriver.findElement(By.xpath("//input[@id='CASE_file_select']"));
    oGeneric.sleepTimer(5);
    Utility.constant.objLogs.writeLog("After finding xpath of  Invisibleelement");



    Utility.constant.objLogs.writeLog("After finding xpath of  file");
    try {
        ofilepathxpath.sendKeys(sfilepath);
        oGeneric.sleepTimer(8);
    }catch (Exception e){
        Utility.constant.objLogs.writeLog("Failed to  choose file");


    }

    Utility.constant.objLogs.writeLog("After choosing file");

I am not sure javascript executor is working on headless browser I have to execute all my scripts on headless browser it is requirement of project. Please let me know solution for this.

Below is the code next to above where script fails

   if(oDriver.findElement(By.xpath(".//*[@id='0_CASE_fileDiv']")).isDisplayed()){
        Utility.constant.objLogs.writeLog("File selected succesfully");

    }else{
        Utility.constant.objLogs.writeLog("File not selected");
    }

    Utility.constant.objLogs.writeLog("After choosing case file");



    String sBtnuploadCaseRelatedFilexpath = (String) constant.hashXPaths.get("btnUploadCaseRelatedFile");
    WebElement oBtnuploadCaseRelatedFile = FindElement(oDriver,sBtnuploadCaseRelatedFilexpath);
    Utility.constant.objLogs.writeLog("After finding xpath of upload btn ");
    oBtnuploadCaseRelatedFile.click();
    oGeneric.sleepTimer(10);
    Utility.constant.objLogs.writeLog("After click on upload btn ");

scripts fails when it will go to if condition bcz upload btn is invisible bcz file is not selected.

sufiya
  • 43
  • 2
  • 8
  • How do you know that headless browser is not working? – Ivan Pronin Apr 10 '17 at 06:20
  • I executed same script on headless browser script will stop at point where I am calling JavascriptExecutor executor so I think it is not working for headless browser. – sufiya Apr 10 '17 at 06:23
  • Why would the script stop at this point? – Ivan Pronin Apr 10 '17 at 06:25
  • How and when, in which line did you instantiate PhantomJsDriver when you tried to test it? Does it throw any exceptions while running your script? – Ivan Pronin Apr 10 '17 at 06:57
  • I am using xvfb not PhantomJs. I am calling one login function I can able to login to my application and all steps are exeuting smoothly except that when my script will come to point where I am checking if(oDriver.findElement(By.xpath(".//*[@id='0_CASE_fileDiv']")).isDisplayed()){} this condition it will throw error xpath not found bcz file not selected – sufiya Apr 10 '17 at 07:06
  • Note that same script is working on normal fire fox browser but fails on headless browser. – sufiya Apr 10 '17 at 07:06
  • After making element visible using javascript I observed one thing on firefox browser that is one browse btn is visible and I think when same javascript execute on headless browser it is not visible, so not able to select file and upload btn is not visible – sufiya Apr 10 '17 at 07:11

0 Answers0