{"id":2097,"date":"2023-06-30T13:09:31","date_gmt":"2023-06-30T11:09:31","guid":{"rendered":"https:\/\/ittester.sk\/sin-categorizar\/selenium-webdriver-lanzamiento-de-navegadores\/"},"modified":"2024-06-25T16:04:56","modified_gmt":"2024-06-25T14:04:56","slug":"selenium-webdriver-lanzamiento-de-navegadores","status":"publish","type":"post","link":"https:\/\/ittester.sk\/es\/sin-categorizar\/tutorial-de-selenio\/selenium-webdriver-lanzamiento-de-navegadores\/","title":{"rendered":"Selenium Webdriver &#8211; lanzamiento de navegadores"},"content":{"rendered":"\n<p>En este art\u00edculo, vamos a echar un vistazo detallado a los comandos de Selenium WebDriver que se utilizan para lanzar navegadores. Tambi\u00e9n aprenderemos las distintas personalizaciones adicionales necesarias para ejecutar determinados navegadores, como Chrome, Firefox, Internet Explorer y Safari.<\/p>\n\n<h2 class=\"wp-block-heading\">\u00cdndice<\/h2>\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a class=\"aioseo-toc-item\" href=\"#aioseo-pochopenie-prikazu-na-spustenie-prehliadaca\">Pochopenie pr\u00edkazu na spustenie prehliada\u010da<\/a><\/li><li><a class=\"aioseo-toc-item\" href=\"#aioseo-spustenie-prehliadaca-firefox\">Spustenie prehliada\u010da Firefox<\/a><\/li><li><a class=\"aioseo-toc-item\" href=\"#aioseo-spustenie-prehliadaca-chrome\">Spustenie prehliada\u010da Chrome<\/a><\/li><li><a class=\"aioseo-toc-item\" href=\"#aioseo-spustenie-prehliadaca-internet-explorer\">Spustenie prehliada\u010da Internet Explorer<\/a><\/li><li><a class=\"aioseo-toc-item\" href=\"#aioseo-spustenie-prehliadaca-safari\">Spustenie prehliada\u010da Safari<\/a><\/li><\/ul><\/div>\n<h2 class=\"wp-block-heading\" id=\"aioseo-pochopenie-prikazu-na-spustenie-prehliadaca\">Comprender el comando para iniciar el navegador<\/h2>\n\n<p>Como hemos mencionado en tutoriales anteriores, Selenium WebDriver llama a m\u00e9todos nativos de varios navegadores para automatizarlos. Por eso tenemos diferentes WebDrivers para diferentes navegadores en Selenium, como &#8211; FirefoxDriver para Firefox, ChromeDriver para Google Chrome, InternetExplorerDriver para Internet Explorer, etc. Ahora tomemos el ejemplo de lanzar Firefox e intentemos comprender el comando en detalle:<\/p>\n\n<p><strong>WebDriver driver = nuevo FirefoxDriver(); <\/strong><\/p>\n\n<p>Esta es la implementaci\u00f3n java del lanzamiento del navegador en Selenium. Aqu\u00ed est\u00e1 la interfaz &#8216;WebDriver&#8217; y estamos creando una variable de referencia &#8216;driver&#8217; de tipo WebDriver, instanciada mediante la clase &#8216;FireFoxDriver&#8217;. Para los que no dominen mucho Java, una interfaz es como un contrato que deben cumplir las clases que la implementan. La interfaz contiene un conjunto de variables y m\u00e9todos sin cuerpo (sin implementaci\u00f3n, s\u00f3lo un nombre de m\u00e9todo y una firma). No podemos instanciar objetos a partir de interfaces.<br\/>Por lo tanto, la siguiente l\u00ednea de c\u00f3digo es incorrecta y lanza un error de compilaci\u00f3n con el mensaje \u00abNo se puede instanciar el tipo WebDriver\u00bb.<\/p>\n\n<p><strong>WebDriver driver = nuevo WebDriver();  <\/strong><\/p>\n\n<p>Para instanciar objetos controlador, necesitamos clases como FirefoxDriver o ChromeDriver que implementen la interfaz WebDriver. En otras palabras, estas clases de controlador siguen el contrato WebDriver implementando todos los m\u00e9todos de la interfaz WebDriver. Esto hace que todos los tipos de clases de conductor sean uniformes y sigan el mismo protocolo.<br\/>Ten en cuenta que tambi\u00e9n podemos crear una variable de referencia de tipo FirefoxDriver:<\/p>\n\n<p><strong>FirefoxDriver controlador = nuevo FirefoxDriver();<\/strong><\/p>\n\n<p>Sin embargo, la existencia de un objeto de referencia WebDriver ayuda cuando se prueban varios navegadores, porque el mismo objeto controlador puede utilizarse para asignar a cualquiera de los controladores espec\u00edficos del navegador deseado.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-spustenie-prehliadaca-firefox\">Iniciar Firefox<\/h2>\n\n<p>Firefox es uno de los navegadores m\u00e1s utilizados en automatizaci\u00f3n. Para iniciar Firefox debes seguir los siguientes pasos:<\/p>\n\n<ol class=\"wp-block-list\">\n<li>Descarga el archivo geckodriver.exe de la p\u00e1gina de publicaci\u00f3n de GeckoDriver en Github. Aseg\u00farate de descargar el archivo del controlador correcto en funci\u00f3n de tu plataforma y versi\u00f3n del sistema operativo.<\/li>\n\n\n\n<li>Establece la<strong>Propiedad<\/strong> del Sistema en tu ordenador para \u00abwebdriver.gecko.driver\u00bb con la ruta geckodriver.exe &#8211; System.setProperty(\u00abwebdriver.gecko.driver\u00bb, \u00abruta geckodriver.exe\u00bb);<\/li>\n<\/ol>\n\n<p>Fragmento para ejecutar Firefox &#8211;<\/p>\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9e9e9\"><code>public class FirefoxBrowserLaunchDemo {\n&nbsp; &nbsp; public static void main(String&#091;] args) {\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Creating a driver object referencing WebDriver interface\n&nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver;\n&nbsp; &nbsp; &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Setting webdriver.gecko.driver property\n&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty(\"webdriver.gecko.driver\", pathToGeckoDriver + \"\\\\geckodriver.exe\");\n&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Instantiating driver object and launching browser\n&nbsp; &nbsp; &nbsp; &nbsp; driver = new FirefoxDriver();\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Using get() method to open a webpage\n&nbsp; &nbsp; &nbsp; &nbsp; driver.get(\"https:\/\/ittester.sk\");\n&nbsp; &nbsp;\/\/Closing the browser\n&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();\n&nbsp; &nbsp; }\n}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-spustenie-prehliadaca-chrome\">Iniciar Chrome<\/h2>\n\n<p>Para ejecutar Chrome en Selenium, tenemos que establecer la propiedad del sistema webdriver.chrome.driver para que apunte al ejecutable del controlador de Chrome:<\/p>\n\n<ol class=\"wp-block-list\">\n<li>Descarga el \u00faltimo binario de ChromeDriver desde la p\u00e1gina de descargas <strong><a href=\"https:\/\/sites.google.com\/a\/chromium.org\/chromedriver\/downloads\" target=\"_blank\" rel=\"noopener nofollow\" title=\"\">P\u00e1gina de descargas de Chromium.org<\/a> <\/strong>y coloca el archivo ejecutable en tu m\u00e1quina local.<\/li>\n\n\n\n<li>Establece la propiedad webdriver.chrome.driver en la ubicaci\u00f3n del archivo chromeDriver.exe como-.<\/li>\n<\/ol>\n\n<p><strong>System.setProperty(\u00abwebdriver.chrome.driver\u00bb, \u00abruta chromeDriver.exe\u00bb);<\/strong><\/p>\n\n<p>Fragmento para iniciar Chrome:<\/p>\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9e9e9\"><code>public class ChromeBrowserLaunchDemo {\n    public static void main(String&#091;] args) {\n        \n        \/\/Creating a driver object referencing WebDriver interface\n        WebDriver driver;\n        \n        \/\/Setting the webdriver.chrome.driver property to its executable's location\n        System.setProperty(\"webdriver.chrome.driver\", \"\/lib\/chromeDriver\/chromedriver.exe\");\n \n        \/\/Instantiating driver object\n        driver = new ChromeDriver();\n        \n        \/\/Using get() method to open a webpage\n        driver.get(\"https:\/\/ittester.sk\");\n        \n        \/\/Closing the browser\n        driver.quit();\n    }\n}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-spustenie-prehliadaca-internet-explorer\">Iniciar Internet Explorer<\/h2>\n\n<p>De forma similar a ChromeDriver, el controlador de InternetExplorer requiere que se establezca la propiedad \u00abwebdriver.ie.driver\u00bb con la ubicaci\u00f3n de IEDriverServer.exe. Puedes descargar el archivo IEDriverServer.exe <strong>\n  <a href=\"https:\/\/selenium-release.storage.googleapis.com\/index.html\" target=\"_blank\" rel=\"noopener nofollow\" title=\"\">aqu\u00ed<\/a>\n<\/strong>.<br\/>Puedes utilizar el siguiente fragmento de c\u00f3digo para iniciar IE:<\/p>\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9e9e9\"><code>public class IEBrowserLaunchDemo {\n    public static void main(String&#091;] args) {\n        \n        \/\/Creating a driver object referencing WebDriver interface\n        WebDriver driver;\n        \n        \/\/Setting the webdriver.ie.driver property to its executable's location\n        System.setProperty(\"webdriver.ie.driver\", \"\/lib\/IEDriverServer\/IEDriverServer.exe\");\n \n        \/\/Instantiating driver object\n        driver = new InternetExplorerDriver();\n        \n        \/\/Using get() method to open a webpage\n        driver.get(\"https:\/\/ittester.sk\");\n        \n        \/\/Closing the browser\n        driver.quit();\n \n    }\n}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"aioseo-spustenie-prehliadaca-safari\">Iniciar Safari<\/h2>\n\n<p>El navegador Safari no requiere ninguna configuraci\u00f3n adicional y puede ser lanzado directamente por una instancia que utilice SafariDriver.<\/p>\n\n<p>El siguiente fragmento de c\u00f3digo puede utilizarse para iniciar Safari:<\/p>\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9e9e9\"><code>public class SafariBrowserLaunchDemo {\n&nbsp; &nbsp; public static void main(String&#091;] args) {\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Creating a driver object referencing WebDriver interface\n&nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver;\n&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Instantiating driver object with SafariDriver\n&nbsp; &nbsp; &nbsp; &nbsp; driver = new SafariDriver();\n&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Using get() method to open a webpage\n&nbsp; &nbsp; &nbsp; &nbsp; driver.get(\"https:\/\/ittester.sk\");\n&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/Closing the browser\n&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();\n&nbsp;\n&nbsp; &nbsp; }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este art\u00edculo, vamos a echar un vistazo detallado a los comandos de Selenium WebDriver [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2098,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-2097","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial-de-selenio"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":1,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/posts\/2097\/revisions"}],"predecessor-version":[{"id":2099,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/posts\/2097\/revisions\/2099"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/media\/2098"}],"wp:attachment":[{"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/media?parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/categories?post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittester.sk\/es\/wp-json\/wp\/v2\/tags?post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}