如何在c#上实现PhantomJS + Webdriver

时间:2013-05-16 20:44:25

标签: selenium-webdriver phantomjs

我已经编写了以下代码并在c:\上指定了文件夹下载了PhantomJS但是我收到错误:“无法连接到远程服务器......”。这是我写的代码:

[TestMethod]
public void HeadlessBrowser()
{
    IWebDriver driver = new PhantomJSDriver("C:\\trashStuff\\phantomjs-1.9.0-windows"); //or some other driver
    driver.Navigate().GoToUrl("http://yahoo.com");

    // Lets take a screenshot to really make sure we did visit the site above
    Console.WriteLine("Take A screen shot");
    Screenshot myScreenShot = ((ITakesScreenshot)driver).GetScreenshot();
    myScreenShot.SaveAsFile("c:\\trashStuff\\screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


    // after the run, go to the location above and find screenshot.jpg
}

1 个答案:

答案 0 :(得分:1)

下载PhantomJS或使用NuGet Package Manager将其添加到您的项目中。我使用NuGet得到它并且不必将路径作为PhantomJSDriver类中的参数之一放入驱动程序。

var driver = new PhantomJSDriver();