{"id":2781,"date":"2023-11-26T13:36:00","date_gmt":"2023-11-26T13:36:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/26\/desiredcapabilities-browseroptions-in-selenium\/"},"modified":"2023-11-26T13:36:00","modified_gmt":"2023-11-26T13:36:00","slug":"desiredcapabilities-browseroptions-in-selenium","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/11\/26\/desiredcapabilities-browseroptions-in-selenium\/","title":{"rendered":"DesiredCapabilities\/BrowserOptions in Selenium"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<ol class=\"wp-block-list\">\n<li>headless launch option<\/li>\n<li>Accept and bypass privacy error<\/li>\n<li>disable info bar<\/li>\n<li>change height\/width of window<\/li>\n<li>run code in incognito mode (private browsing)<\/li>\n<li>launch browser in mobile view<\/li>\n<\/ol>\n<p><strong>headless launch option<\/strong><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p>Let us google search \u201cupgrade to selenium 4\u201d<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ULvC8JzjtX0yj2HyV60aB2_yoUZFm_yry3eNEBzF95P6hmj9cXql1JPlVmAWaa_SmnKDbJBhndH3iKwWE4SulIkunUKNX1Kw8fBOxUcmTwk_xO8syK0dvuztZlMcPmea0xZnScRezIRtumagwbTZPweE-CQkBWMw0792IlR10fY4jrIhvXgC3JH9AAKAnAE\"\/><\/figure>\n<p>Click the first search result link <a href=\"https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/\" rel=\"noopener\" target=\"_blank\">https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/<\/a>\u00a0<\/p>\n<p>Notice below that the DesiredCapabilities is now kind of deprecated. It is replaced by browserOptions() classes like FirefoxOptions, ChromeOptions etc<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/u8iyjZHvRB8VovWzGwVkztVxwukc0nAduHH2oj8S5ZWG_OeKPInoCvdUGJmudGkxuNxZbIGQYE7m7Wqg-2LdWCxP7v4OH7enppDvPoL7cOUXUZggsjDOLvzbttBu6nJt6_z7UOCpgUiuS5CkivtiaxgXw5t9kLWvGURTKIc9DEHdhtLAe8Kxli3PUIOOMTI\"\/><\/figure>\n<p>Let us see how some of these options work. Launch <a href=\"https:\/\/rahulshettyacademy.com\/\">https:\/\/rahulshettyacademy.com\/<\/a> to find out the page title<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/uZxYKBP5DuseZL_UW_uV_ytGLos3wrSkHSDiFs4WYVB4LfeKpmBXIIS82V9RsRVN_C4qaemW66otgEuGVUOpMluzFGXarG3sDaeQxfzsC54oovGnprGoAdrk3BKfumJKHi9dUbAPHrbK7U_-2xs7-FT-lNcj0JkTP19XTTMkbAVZi8jl65dbr-nDLHSV4fg\"\/><\/figure>\n<p>In line#12 below, we are creating a reference \u2018<strong>o<\/strong>\u2019 of <em>FirefoxOptions()<\/em> and passing this reference as constructor argument in line#16\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/i93_S5Vx72xJf_Jqts3Qv2MgBjIqGWpmNFefipCRmgXnlHlhnDthR1EUf_jSyc7BDrDUMhx-6gbrvDDfh3TJZGl6_-4779PCHxSyV5fO0NABkx89D0rmXeHmMozDEbAOGSw-eXSgUUPWDKe0C3TbbyQdxvy4H2sryS8nRx_d82fpp2WUW2bxVfx8BRaNTUw\"\/><\/figure>\n<p>We have invoked the <em>headless<\/em> in line#13.<\/p>\n<p>When we run our code, the browser does not launch and the page title gets printed<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/0Fc1lGFyvhg43Plal9J6MV7cutgz3TXhhMuky_YEIqkvYLSZD7FEM_jyVDcS7T2aLolav9nTmgWURUQVmpagCJNM7H8TI-zrxLgeb99mKeOum18eJifkriOFVqHhXTDHNTieS901car3PouuMOrvp8AGUSX6RmMnHJTY4P_9KkgKczUkNfGTBl75AWe3VBA\"\/><\/figure>\n<p>The exact same thing can be achieved using ChromeOptions() as well, see below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/4ZM_r7cDzdJDnnsM3cwzvav6HqfC_TmzLenEk8W13yatn3iw-ttRPLfSkQPvKAuCcEBBB002pmrV_XuxfEiQ2xyjjV300kRy96x9_Rcjl-F4tH6rv-QJ2Jei6qaQ8RWTBhXys3j3Ef7519dt1aNILT19iRGOcUFUCUfsuW4fptzj7FSldU7vuiXGf3Y0JQk\"\/><\/figure>\n<p>Before we move ahead, let us comment<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/UXlDL8aJMpFqMH236zk9q6snd6AnhlWEQSVqbtlJRZ4q4UzKRXqecapinI5T9EPzVUe9bmYA_zaZXUxswb1TnM8XXG7GdVHgahB5RBKa3OiwQeAXDbTKEX3WPWS0U8894rEr-Fzu1SiT7pA_1ql9DqSOftkfE7uDW8SPbZl9yQJBlhixZAoj8NMFQOPXfU0\"\/><\/figure>\n<p><strong>Accept and bypass privacy error<\/strong><\/p>\n<p>Launch <a href=\"https:\/\/expired.badssl.com\/\" rel=\"noopener\" target=\"_blank\">https:\/\/expired.badssl.com\/<\/a>\u00a0<\/p>\n<p>Below certificate error is seen and to bypass this privacy error, we have to perform some manual steps<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/IRQqu2b9J31jQWh6AR8AQEyU0FprFVggU5zcGO7BmBklQxaZ_CY8sA3VKWZbudyzoNaYqdIIPFN1fERuF9nJBWG-2EANQYqfDDc51fSPLTUAymSKKG14LoH8a2rHG6dH9Ld7pUh4fEb4YezOa-riDoBMuZOoTA9V55XXaHoAoOqMUvEQLHN0-J7EAkpkpV4\"\/><\/figure>\n<p>Let us try to run it through selenium and see if we can bypass the privacy error<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/iw8mKoJobmueieVzw-_pHocBHPOW7rivWfEdMpnndlTlStY1b6oHmbr4wvL4HndhcVOX3QMABiUf0GUbUesitEbbzKnIuYwidbaa1tCD_usuQwLQUXNwRJy27vfQ6xgPiCYTqJvr1gH1z_9SsaIew_4fb7d6lh7M-VzL6FBOSsNjAWTW0s59UUuc294m1o4\"\/><\/figure>\n<p>Run, see below, we still get the privacy error\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fpf7hUP1whTwDD2kiU9GuJqq3isgnVV1ntoMTOgy6XHb5Vv6lXb1ikATH0BCKXrm8ZHLj-3MgsUuP3Jk7x-iY1JJQObC_l7RmfdQpM-CJxDDr4WnEy2ojLQewF-376iTAnQDangNcrmzOjhEJwjlP0YXLpBk-TbiOV8C-JmapBYvOIT7cpskrhCd2K6ZkJU\"\/><\/figure>\n<p>To bypass the privacy error, we have to simply use the below option and set it to \u2018true\u2019\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CA68VKdxvBtPp6gxj73io3cLq8qDKS7a3dtM7RcZJYpiXx3kCr7uEa-91aXBRZ_4Dq0iLJ-iZL4GexESmfciQf9FVS2DJXT34xzUAnnINQIvsmVAjyra_Z2LifbbnAw7kPtCWIshkguz71W3R1TPmEZYwiaGZgz-2_qOvDysT3WvAhJXonr-Mygssqrv_nM\"\/><\/figure>\n<p>Now when we run, the error gets bypassed, see below<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/YoYQ4VkY4FpLvBPTfIpIy66cUQtJuWTP9hNRKaKsOAzUEW4tzWBwq6btvsqOgJLR_9DBiwS5iakbujyXBfKOETP6MNv27KKbuendn1e3D4nqh431mGgFQZqVoNvwQCkEuTtY24qIocawvW4H7Th-RwhfCluF2eMcQLcjH_RqgUCjXflxS0913KlpYl2OeRM\"\/><\/figure>\n<p><strong>disable info bar<\/strong><\/p>\n<p>When an automation test launches a browser, there is an info bar that is visible, see below, \u201cChrome is being controlled by automated test software\u201d\u00a0<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ONoUYFQbJJX9b3j3lyGzZyHhpSwC2u7VM1hlQVDLVA9sE4V-unykGvcmsrK3-z50sXuP7DOtvrKKziWBu7xyoe42CQdWkWnvpPLUdLeUQRZFRDmfAQa_PuV09GPYO3k28EGbsBC-mkspuLQO41KV9RRs-lAwYdWxxyE0Yc8vB1SM7a1e0yEjoax204GUL-M\"\/><\/figure>\n<p>We can disable this info bar by setting the option:<\/p>\n<p><em>o<\/em><em>.setExperimentalOption(<\/em><em>\u201cexcludeSwitches\u201d<\/em><em>, Collections.singletonList(<\/em><em>\u201cenable-automation\u201d<\/em><em>));<\/em><\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/irlk02cdF6rIX7nFDbPnzgZ5p85qT31-8Jkme68RoNqNzNyNqvBWZ5HyNKvyGSvtDvx7jVQZ2X0t3_ooTjiEoNyWTJfu9ksoJ0nuZ3I3TfDH_8z4Gj2SfRb38KBFS0MnaWPTaywWefrQ6PnSxroHYYifFTdTfABV5OlRVlm_b4nMTlUaGASVElxZ7R1dpsc\"\/><\/figure>\n<p>Run, now we don\u2019t see the info bar<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/oEyMsTUdzxfCdujRRhml5tsYtt01y0HNv2xKemqoh7fwM9si4Qlkuaos5TZKGKzle9szzg3U93ZZ4bQQKLRwOn7nvEtNJkZC6LBRD2kBUASGIk64hmh20vBczgU7CLZqd7wmu5tQgg4KvElLFVYqiinZm0BxM-78Ok6hn2VzTzI1R6Djygoc5mZXBY1d6L4\"\/><\/figure>\n<p><strong>change height\/width of window<\/strong><\/p>\n<p>We can change the window dimensions by setting the highlighted option<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ydtwGAULEjgPD8zkpcPbS-YCdBFbHVwoJbPKXyxNxklAuQBkuTsyK50mI1QFAKMq5zp4eSBFu0eXW7ZHHZ1PUR5tuJnNUA9yWjok0GyXrbBjc6CRlPEv9v5mzOFyCJ-pYmRo5OOK7MC4eGTmxDVPT8wsxP-jpSzs4iiFQrSm57r3yeQmZBpC48s6LPUPuaU\"\/><\/figure>\n<p>Run, the window dimension changes<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6xsl2islCRI0yjo46Qv3IA1ro7bMEDx_c4ZOqb4t-hLac-0o5AfD58_MxO-qeNpMWPcccdQG77L7IDiQCxXuIVlrw6_cxqVfDyVNFXWKkaJHyFfyLaM_GF3YWBWj4HhDTL6murzvjZOnWmfyhJuMgDiFf7NydZ2T09KtTgJaHGG1xWckk9Uer5_g_o3tMQ0\"\/><\/figure>\n<p><strong>run code in incognito mode (private browsing)<\/strong><\/p>\n<p>The below setting can be used to open the chrome in incognito mode:<\/p>\n<p><em>o<\/em><em>.addArguments(<\/em><em>\u201cincognito\u201d<\/em><em>);<\/em><\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/_5K8TmJNUNMUjXx6sU76ImDWveIpqSPtvGH25c9Vb0apEWkcq40ksORh8FsZUrvwKX8AfSi7zxX0HvmBnFSiDFqZU7tOpxyh6GIctxQ_RPi8z03PlOdqBImpL7fTT2Hbelzl46IAUW054qWs3btliZKmuvwhb9u8EUnDfCHPDPEw5HWNobS-S7YvRw9NzPI\"\/><\/figure>\n<p><strong>launch browser in mobile view<\/strong><\/p>\n<p>Let us say we want to launch website in iPhone-x mode.<\/p>\n<p>We can do that by first creating a hashmap reference and then setting the reference as experimental option (see line#28)<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/c8S14gwH5babSaW9hnk6HO2hymnXNpX_lbEVxC0OdfNhVU4kGpBpwwXYpuU4hp-VEW30pyL9J1OQV87DONnJP3ICRMILA_N15luVmUjlY3F6t7sMkY_psEE7-Ydnt1VdhzxfXf47sek2dcOCUk_91rHEg8zykak5LPTU-g963wUDCqog4r2i-2ZOkUHQ8_U\"\/><\/figure>\n<p>Run, see below. The browser launches in iPhone mode and the website is displayed as if we are opening the website in iPhone<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/GVzTlEPK2Q_PQXhD8O85MLSQmb3KZuzzrPpG40c9onrAbSC0Q0wcrvfbVVC7omuhNMqBx7SJo-Peh5x1KUPavVnxhmeOLO8HxUXQ64EE51grN__pzhZY9Q0zZMAgmGMct8HKgq_6QyE_4nZI6UelqS0R5_Fm02EfP3QtcN6qYfr1A0oBXK7vcn1cS78Ym-c\"\/><\/figure>\n<p>Below is source code snapshot<\/p>\n<figure class=\"wp-block-image\"><img alt=\"\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/PZYU5S17IUKBT5psZiY73MAXrFQpI56PT5Ku_kQ0pni5dFskra3MB_pdqLQwkfRqeMLPgX-2s7lNOQ1iewBgRpXbcGnd4cJqx2LZYBZ5TZp5K0JB2NZV4EyZrxyuJp6-AGr_cZdTHWPXpUYKUl4JBXzcV6A5TO-jeltQJZo_Dw4DkhEDd41kjQwy7oD4qVc\"\/><\/figure>\n<p>So, that\u2019s how we can manipulate different behaviours of our browser.<\/p>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>headless launch option Accept and bypass privacy error disable info bar change height\/width of window run code in incognito mode (private browsing) launch browser in mobile view headless launch option Let us google search \u201cupgrade to selenium 4\u201d Click the first search result link https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/\u00a0 Notice below that the DesiredCapabilities is now kind of deprecated. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2781","post","type-post","status-publish","format-standard","hentry","category-selenium","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2781"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2781\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}