Buscar..


PhantomJS [C #]

PhantomJS es un navegador web sin cabeza con todas las funciones con el soporte de JavaScript.

Antes de comenzar, deberá descargar un controlador PhantomJS y asegurarse de poner esto al principio de su código:

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;

Genial, ahora en la inicialización:

var driver = new PhantomJSDriver();

Esto simplemente creará una nueva instancia de la clase PhantomJSDriver. Luego puede usarlo de la misma manera que todos los WebDriver, como:

using (var driver = new PhantomJSDriver())
{
    driver.Navigate().GoToUrl("http://stackoverflow.com/");

    var questions = driver.FindElements(By.ClassName("question-hyperlink"));

    foreach (var question in questions)
    {
        // This will display every question header on StackOverflow homepage.
        Console.WriteLine(question.Text);
    }
}

Esto funciona bien. Sin embargo, el problema que probablemente encontró es que, cuando trabaja con la interfaz de usuario, PhantomJS abre una nueva ventana de consola, que en la mayoría de los casos no es realmente necesaria. Afortunadamente, podemos ocultar la ventana e incluso mejorar ligeramente el rendimiento utilizando PhantomJSOptions y PhantomJSDriverService . Ejemplo de trabajo completo a continuación:

// Options are used for setting "browser capabilities", such as setting a User-Agent
// property as shown below:
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", 
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");

// Services are used for setting up the WebDriver to your likings, such as
// hiding the console window and restricting image loading as shown below:
var service = PhantomJSDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
service.LoadImages = false;

// The same code as in the example above:
using (var driver = new PhantomJSDriver(service, options))
{
    driver.Navigate().GoToUrl("http://stackoverflow.com/");

    var questions = driver.FindElements(By.ClassName("question-hyperlink"));

    foreach (var question in questions)
    {
        // This will display every question header on StackOverflow homepage.
        Console.WriteLine(question.Text);
    }
}

PhantomJSDriverService profesional: haga clic en una clase (por ejemplo, PhantomJSDriverService ) y presione F12 para ver exactamente lo que contienen junto con una breve descripción de lo que hacen.

SimpleBrowser [C #]

SimpleBrowser es un WebDriver ligero sin soporte de JavaScript.

Es considerablemente más rápido que un PhantomJS mencionado PhantomJS , sin embargo, cuando se trata de funcionalidad, se limita a tareas simples sin funciones sofisticadas.

En primer lugar, deberá descargar el paquete SimpleBrowser.WebDriver y luego colocar este código al principio:

using OpenQA.Selenium;
using SimpleBrowser.WebDriver;

Ahora, aquí hay un breve ejemplo de cómo usarlo:

using (var driver = new SimpleBrowserDriver())
{
    driver.Navigate().GoToUrl("http://stackoverflow.com/");

    var questions = driver.FindElements(By.ClassName("question-hyperlink"));

    foreach (var question in questions)
    {
        // This will display every question header on StackOverflow homepage.
        Console.WriteLine(question.Text);
    }
}

HTMLUnitDriver

HTMLUnitDriver es la implementación más liviana de un navegador sin cabeza (sin GUI) para Webdriver basado en HtmlUnit. Modela documentos HTML y proporciona una API que le permite invocar páginas, completar formularios, hacer clic en enlaces, etc., tal como lo hace en su navegador normal. Es compatible con JavaScript y funciona con las bibliotecas AJAX. Se utiliza para probar y recuperar datos del sitio web.


Ejemplo: uso de HTMLUnitDriver para obtener una lista de preguntas de http://stackoverflow.com/ .


import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
        
class testHeadlessDriver{
            private void getQuestions() {
                    WebDriver driver = new HtmlUnitDriver();
                    driver.get("http://stackoverflow.com/");
                    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                    List<WebElement> questions = driver.findElements(By.className("question-hyperlink"));
                    questions.forEach((question) -> {
                        System.out.println(question.getText());
                    });
                   driver.close();
                }
    }

Es igual que cualquier otro navegador (Mozilla Firefox, Google Chrome, IE), pero no tiene GUI, la ejecución no está visible en la pantalla.



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow