How to configure and setup HTMLUNIT with Selenium while using it in C#?

718 views Asked by At

I setup selenium remote driver and run the selenium server.The selenium server running correctly and while I try to run my code using :

        var remoteServer = new Uri("http://127.0.0.1:4444/wd/hub");
        DesiredCapabilities desiredCapabilities = DesiredCapabilities.Firefox();
        desiredCapabilities.IsJavaScriptEnabled = true;
 myDriver = new RemoteWebDriver(remoteServer, desiredCapabilities, new TimeSpan(0,1, 30));

No error throws in the CMD log and elements can find properly, but headache comes while I try to run this using the below code :

      var remoteServer = new Uri("http://127.0.0.1:4444/wd/hub");
       DesiredCapabilities desiredCapabilities = DesiredCapabilities.HtmlUnit();
       desiredCapabilities.IsJavaScriptEnabled = true;
       myDriver = new RemoteWebDriver(remoteServer, desiredCapabilities, new TimeSpan(0, 1, 30));

in log it throws lots of error and while I try to find any element, timeout exception showing in log. My test code is in below :

        myDriver.Manage().Window.Maximize();
        myDriver.Navigate().GoToUrl(tollFreeURL);
        IWebElement planClick = myDriver.FindElement(By.ClassName("trial"));
        planClick.Click();
        IWebElement startPlan = myDriver.FindElement(By.Id("choose2000"));
        startPlan.Click();
        IWebElement selectValue = myDriver.FindElement(By.Name("AreaCode"));
        var selectElement = new SelectElement(selectValue);
        selectElement.SelectByValue("800");
 IWebElement selectNumber = myDriver.FindElement(By.XPath("//*[@id='divList']/div[1]"));
        tollFreeNumber = selectNumber.Text;
        tollFreeNumber = stringConvert.StringRefiner(tollFreeNumber, " ");
        tollFreeNumber = stringConvert.StringRefiner(tollFreeNumber, "(");
        tollFreeNumber = stringConvert.StringRefiner(tollFreeNumber, ")");
        tollFreeNumber = stringConvert.StringRefiner(tollFreeNumber, "-");
        Console.WriteLine(tollFreeNumber);

My target is to run the application in background and generate data, so I do not have any option to run this using Firefox.HTMLUNIT is the only option for me.Help needed it will stuck me last 3-4 days.

1

There are 1 answers

0
Surya On

I believe your machine is using proxy servers. Try by adding that proxy configuration while creating driver instance.

String PROXY = "xx.xx.xx.xx:8080"; 
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;

Add proxy setting to desired Capabilities:

desiredCapabilities.SetCapability(CapabilityType.PROXY, proxy);