-2

I need to implement Headless Browser for HTTPS (validate certificate). For this I need to write extra line of code.

I have written for browser HTTP and it is working fine.

public class Headless {

  public static void main(String[] args)

 {

File src=new File("C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");

  System.setProperty("phantomjs.binary.path",src.getAbsolutePath());

  WebDriver driver=new PhantomJSDriver();

  driver.get("https://www.google.co.in/");

  System.out.println(driver.getTitle());

 }

}

I google it and get some info:-

phantomjs --ignore-ssl-errors=yes;
halfer
  • 19,824
  • 17
  • 99
  • 186
Vivek
  • 19
  • 3
  • please format your code... – karan Aug 26 '16 at 05:44
  • HI Vivek, and welcome to Stack Overflow.Could you describe in some more detail how far you've got i.e. what you've tried so far, and what about it isn't working? Remember you can [edit] your question to include more information. – Vince Bowdren Aug 28 '16 at 10:18
  • Instead of "PhantomJS" I have used "HtmlUnitDriver" and unable to type to Username text box. WebDriver driver = new HtmlUnitDriver(); driver.get("https://XXXX.northamerica.abc.net:8443/#/login"); System.out.println(driver.getTitle()); driver.findElement(By.xpath("//input[@class='ember-view ember")).sendKeys("XXXXXX"); Unable to type to Text box.Can any one check,Why is it not working for headless browser.I am able to execute it for Mozilla browser. – Vivek Aug 29 '16 at 17:57
  • While Launching browser through "PhantunJS" driver then browser is not opening.I saw in screen shot. – Vivek Aug 30 '16 at 11:02
  • While Launching browser through "PhantunJS" driver then browser is not opening.I saw in screen shot. But while launching via "HtmlUnitDriver()" then I am able to launch browser but unable to type in username text box.so over came this i tried to take screen shot but it is throwing the below error message.Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.htmlunit.HtmlUnitDriver cannot be cast to org.openqa.selenium.TakesScreenshot – Vivek Aug 30 '16 at 12:08
  • I have commented screen shot step and able to proceed with HTMLUnit driver.......I got stuck in one page........Able to locate the element while executing script for "MozillaBrowser driver" but for same element unable to locate while executing script for "HTMLUnit driver"....it is throwing error message telling that unable to locate element........ – Vivek Sep 01 '16 at 06:28
  • if ( !driver.findElement(By.xpath("(//input[@type='checkbox'])[1]")).isSelected() ) { driver.findElement(By.xpath("(//input[@type='checkbox'])[1]")).click(); } if ( !driver.findElement(By.xpath("(//input[@type='checkbox'])[2]")).isSelected() ) { driver.findElement(By.xpath("(//input[@type='checkbox'])[2]")).click(); } – Vivek Sep 01 '16 at 06:28
  • It is recognizing first element but unable to recognize the second element.......It is working perfectly for "Mozilla browser driver" but it is throwing error message for "HtmlUnit driver".....Plz look into it – Vivek Sep 01 '16 at 06:28

1 Answers1

0
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] {
    "--ssl-protocol=any",
    "--ignore-ssl-errors=true"
};

dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);

PhantomJSDriver driver = new PhantomJSDriver(dcap);

You can read more about the command-line options on this link: Command Line Interface | PhantomJS

peetya
  • 3,578
  • 2
  • 16
  • 30
  • Where do i need to include the above piece of code in the above program. – Vivek Aug 26 '16 at 12:22
  • We need to use "WebDriver" as well. – Vivek Aug 26 '16 at 12:23
  • PhantomJSDriver is an instance of WebDriver so you can use it. – peetya Aug 29 '16 at 11:55
  • Instead of "PhantomJS" I have used "HtmlUnitDriver" and unable to type to Username text box. WebDriver driver = new HtmlUnitDriver(); driver.get("XXXX.northamerica.abc.net:8443/#/login"); System.out.println(driver.getTitle()); driver.findElement(By.xpath("//input[@class='ember-view ember")).sendKeys("XXXXXX"); Unable to type to Text box.Can any one check,Why is it not working for headless browser.I am able to execute it for Mozilla browser – Vivek Sep 07 '16 at 06:27
  • While Launching browser through "PhantunJS" driver then browser is not opening.I saw in screen shot. – Vivek Aug 30 at 11:02 While Launching browser through "PhantunJS" driver then browser is not opening.I saw in screen shot. But while launching via "HtmlUnitDriver()" then I am able to launch browser but unable to type in username text box.so over came this i tried to take screen shot but it is throwing the below error message.Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.htmlunit.HtmlUnitDriver cannot be cast to org.openqa.selenium.TakesScreenshot – Vivek Sep 07 '16 at 06:28
  • at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementById(HtmlUnitDriver.java:998) at org.openqa.selenium.By$ById.findElement(By.java:218) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1705) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1701) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1347) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1701) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:586) at – Vivek Sep 07 '16 at 06:28