{"id":2781,"date":"2023-11-26T13:36:00","date_gmt":"2023-11-26T13:36:00","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2781"},"modified":"2023-11-19T16:37:23","modified_gmt":"2023-11-19T16:37:23","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\u00a0"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>headless launch option<\/li>\n\n\n\n<li>Accept and bypass privacy error<\/li>\n\n\n\n<li>disable info bar<\/li>\n\n\n\n<li>change height\/width of window<\/li>\n\n\n\n<li>run code in incognito mode (private browsing)<\/li>\n\n\n\n<li>launch browser in mobile view<\/li>\n<\/ol>\n\n\n\n<p><strong>headless launch option<\/strong><\/p>\n\n\n\n<p>Let us google search &#8220;upgrade to selenium 4&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ULvC8JzjtX0yj2HyV60aB2_yoUZFm_yry3eNEBzF95P6hmj9cXql1JPlVmAWaa_SmnKDbJBhndH3iKwWE4SulIkunUKNX1Kw8fBOxUcmTwk_xO8syK0dvuztZlMcPmea0xZnScRezIRtumagwbTZPweE-CQkBWMw0792IlR10fY4jrIhvXgC3JH9AAKAnAE\" alt=\"\"\/><\/figure>\n\n\n\n<p>Click the first search result link <a href=\"https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/<\/a>&nbsp;<\/p>\n\n\n\n<p>Notice below that the DesiredCapabilities is now kind of deprecated. It is replaced by browserOptions() classes like FirefoxOptions, ChromeOptions etc<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/u8iyjZHvRB8VovWzGwVkztVxwukc0nAduHH2oj8S5ZWG_OeKPInoCvdUGJmudGkxuNxZbIGQYE7m7Wqg-2LdWCxP7v4OH7enppDvPoL7cOUXUZggsjDOLvzbttBu6nJt6_z7UOCpgUiuS5CkivtiaxgXw5t9kLWvGURTKIc9DEHdhtLAe8Kxli3PUIOOMTI\" alt=\"\"\/><\/figure>\n\n\n\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\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/uZxYKBP5DuseZL_UW_uV_ytGLos3wrSkHSDiFs4WYVB4LfeKpmBXIIS82V9RsRVN_C4qaemW66otgEuGVUOpMluzFGXarG3sDaeQxfzsC54oovGnprGoAdrk3BKfumJKHi9dUbAPHrbK7U_-2xs7-FT-lNcj0JkTP19XTTMkbAVZi8jl65dbr-nDLHSV4fg\" alt=\"\"\/><\/figure>\n\n\n\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&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/i93_S5Vx72xJf_Jqts3Qv2MgBjIqGWpmNFefipCRmgXnlHlhnDthR1EUf_jSyc7BDrDUMhx-6gbrvDDfh3TJZGl6_-4779PCHxSyV5fO0NABkx89D0rmXeHmMozDEbAOGSw-eXSgUUPWDKe0C3TbbyQdxvy4H2sryS8nRx_d82fpp2WUW2bxVfx8BRaNTUw\" alt=\"\"\/><\/figure>\n\n\n\n<p>We have invoked the <em>headless<\/em> in line#13.<\/p>\n\n\n\n<p>When we run our code, the browser does not launch and the page title gets printed<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/0Fc1lGFyvhg43Plal9J6MV7cutgz3TXhhMuky_YEIqkvYLSZD7FEM_jyVDcS7T2aLolav9nTmgWURUQVmpagCJNM7H8TI-zrxLgeb99mKeOum18eJifkriOFVqHhXTDHNTieS901car3PouuMOrvp8AGUSX6RmMnHJTY4P_9KkgKczUkNfGTBl75AWe3VBA\" alt=\"\"\/><\/figure>\n\n\n\n<p>The exact same thing can be achieved using ChromeOptions() as well, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/4ZM_r7cDzdJDnnsM3cwzvav6HqfC_TmzLenEk8W13yatn3iw-ttRPLfSkQPvKAuCcEBBB002pmrV_XuxfEiQ2xyjjV300kRy96x9_Rcjl-F4tH6rv-QJ2Jei6qaQ8RWTBhXys3j3Ef7519dt1aNILT19iRGOcUFUCUfsuW4fptzj7FSldU7vuiXGf3Y0JQk\" alt=\"\"\/><\/figure>\n\n\n\n<p>Before we move ahead, let us comment<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/UXlDL8aJMpFqMH236zk9q6snd6AnhlWEQSVqbtlJRZ4q4UzKRXqecapinI5T9EPzVUe9bmYA_zaZXUxswb1TnM8XXG7GdVHgahB5RBKa3OiwQeAXDbTKEX3WPWS0U8894rEr-Fzu1SiT7pA_1ql9DqSOftkfE7uDW8SPbZl9yQJBlhixZAoj8NMFQOPXfU0\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Accept and bypass privacy error<\/strong><\/p>\n\n\n\n<p>Launch <a href=\"https:\/\/expired.badssl.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/expired.badssl.com\/<\/a>&nbsp;<\/p>\n\n\n\n<p>Below certificate error is seen and to bypass this privacy error, we have to perform some manual steps<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/IRQqu2b9J31jQWh6AR8AQEyU0FprFVggU5zcGO7BmBklQxaZ_CY8sA3VKWZbudyzoNaYqdIIPFN1fERuF9nJBWG-2EANQYqfDDc51fSPLTUAymSKKG14LoH8a2rHG6dH9Ld7pUh4fEb4YezOa-riDoBMuZOoTA9V55XXaHoAoOqMUvEQLHN0-J7EAkpkpV4\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us try to run it through selenium and see if we can bypass the privacy error<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/iw8mKoJobmueieVzw-_pHocBHPOW7rivWfEdMpnndlTlStY1b6oHmbr4wvL4HndhcVOX3QMABiUf0GUbUesitEbbzKnIuYwidbaa1tCD_usuQwLQUXNwRJy27vfQ6xgPiCYTqJvr1gH1z_9SsaIew_4fb7d6lh7M-VzL6FBOSsNjAWTW0s59UUuc294m1o4\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run, see below, we still get the privacy error&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/fpf7hUP1whTwDD2kiU9GuJqq3isgnVV1ntoMTOgy6XHb5Vv6lXb1ikATH0BCKXrm8ZHLj-3MgsUuP3Jk7x-iY1JJQObC_l7RmfdQpM-CJxDDr4WnEy2ojLQewF-376iTAnQDangNcrmzOjhEJwjlP0YXLpBk-TbiOV8C-JmapBYvOIT7cpskrhCd2K6ZkJU\" alt=\"\"\/><\/figure>\n\n\n\n<p>To bypass the privacy error, we have to simply use the below option and set it to \u2018true\u2019&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/CA68VKdxvBtPp6gxj73io3cLq8qDKS7a3dtM7RcZJYpiXx3kCr7uEa-91aXBRZ_4Dq0iLJ-iZL4GexESmfciQf9FVS2DJXT34xzUAnnINQIvsmVAjyra_Z2LifbbnAw7kPtCWIshkguz71W3R1TPmEZYwiaGZgz-2_qOvDysT3WvAhJXonr-Mygssqrv_nM\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now when we run, the error gets bypassed, see below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/YoYQ4VkY4FpLvBPTfIpIy66cUQtJuWTP9hNRKaKsOAzUEW4tzWBwq6btvsqOgJLR_9DBiwS5iakbujyXBfKOETP6MNv27KKbuendn1e3D4nqh431mGgFQZqVoNvwQCkEuTtY24qIocawvW4H7Th-RwhfCluF2eMcQLcjH_RqgUCjXflxS0913KlpYl2OeRM\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>disable info bar<\/strong><\/p>\n\n\n\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&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ONoUYFQbJJX9b3j3lyGzZyHhpSwC2u7VM1hlQVDLVA9sE4V-unykGvcmsrK3-z50sXuP7DOtvrKKziWBu7xyoe42CQdWkWnvpPLUdLeUQRZFRDmfAQa_PuV09GPYO3k28EGbsBC-mkspuLQO41KV9RRs-lAwYdWxxyE0Yc8vB1SM7a1e0yEjoax204GUL-M\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can disable this info bar by setting the option:<\/p>\n\n\n\n<p><em>o<\/em><em>.setExperimentalOption(<\/em><em>&#8220;excludeSwitches&#8221;<\/em><em>, Collections.singletonList(<\/em><em>&#8220;enable-automation&#8221;<\/em><em>));<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/irlk02cdF6rIX7nFDbPnzgZ5p85qT31-8Jkme68RoNqNzNyNqvBWZ5HyNKvyGSvtDvx7jVQZ2X0t3_ooTjiEoNyWTJfu9ksoJ0nuZ3I3TfDH_8z4Gj2SfRb38KBFS0MnaWPTaywWefrQ6PnSxroHYYifFTdTfABV5OlRVlm_b4nMTlUaGASVElxZ7R1dpsc\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run, now we don\u2019t see the info bar<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/oEyMsTUdzxfCdujRRhml5tsYtt01y0HNv2xKemqoh7fwM9si4Qlkuaos5TZKGKzle9szzg3U93ZZ4bQQKLRwOn7nvEtNJkZC6LBRD2kBUASGIk64hmh20vBczgU7CLZqd7wmu5tQgg4KvElLFVYqiinZm0BxM-78Ok6hn2VzTzI1R6Djygoc5mZXBY1d6L4\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>change height\/width of window<\/strong><\/p>\n\n\n\n<p>We can change the window dimensions by setting the highlighted option<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ydtwGAULEjgPD8zkpcPbS-YCdBFbHVwoJbPKXyxNxklAuQBkuTsyK50mI1QFAKMq5zp4eSBFu0eXW7ZHHZ1PUR5tuJnNUA9yWjok0GyXrbBjc6CRlPEv9v5mzOFyCJ-pYmRo5OOK7MC4eGTmxDVPT8wsxP-jpSzs4iiFQrSm57r3yeQmZBpC48s6LPUPuaU\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run, the window dimension changes<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/6xsl2islCRI0yjo46Qv3IA1ro7bMEDx_c4ZOqb4t-hLac-0o5AfD58_MxO-qeNpMWPcccdQG77L7IDiQCxXuIVlrw6_cxqVfDyVNFXWKkaJHyFfyLaM_GF3YWBWj4HhDTL6murzvjZOnWmfyhJuMgDiFf7NydZ2T09KtTgJaHGG1xWckk9Uer5_g_o3tMQ0\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>run code in incognito mode (private browsing)<\/strong><\/p>\n\n\n\n<p>The below setting can be used to open the chrome in incognito mode:<\/p>\n\n\n\n<p><em>o<\/em><em>.addArguments(<\/em><em>&#8220;incognito&#8221;<\/em><em>);<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/_5K8TmJNUNMUjXx6sU76ImDWveIpqSPtvGH25c9Vb0apEWkcq40ksORh8FsZUrvwKX8AfSi7zxX0HvmBnFSiDFqZU7tOpxyh6GIctxQ_RPi8z03PlOdqBImpL7fTT2Hbelzl46IAUW054qWs3btliZKmuvwhb9u8EUnDfCHPDPEw5HWNobS-S7YvRw9NzPI\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>launch browser in mobile view<\/strong><\/p>\n\n\n\n<p>Let us say we want to launch website in iPhone-x mode.<\/p>\n\n\n\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\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/c8S14gwH5babSaW9hnk6HO2hymnXNpX_lbEVxC0OdfNhVU4kGpBpwwXYpuU4hp-VEW30pyL9J1OQV87DONnJP3ICRMILA_N15luVmUjlY3F6t7sMkY_psEE7-Ydnt1VdhzxfXf47sek2dcOCUk_91rHEg8zykak5LPTU-g963wUDCqog4r2i-2ZOkUHQ8_U\" alt=\"\"\/><\/figure>\n\n\n\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\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/GVzTlEPK2Q_PQXhD8O85MLSQmb3KZuzzrPpG40c9onrAbSC0Q0wcrvfbVVC7omuhNMqBx7SJo-Peh5x1KUPavVnxhmeOLO8HxUXQ64EE51grN__pzhZY9Q0zZMAgmGMct8HKgq_6QyE_4nZI6UelqS0R5_Fm02EfP3QtcN6qYfr1A0oBXK7vcn1cS78Ym-c\" alt=\"\"\/><\/figure>\n\n\n\n<p>Below is source code snapshot<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/PZYU5S17IUKBT5psZiY73MAXrFQpI56PT5Ku_kQ0pni5dFskra3MB_pdqLQwkfRqeMLPgX-2s7lNOQ1iewBgRpXbcGnd4cJqx2LZYBZ5TZp5K0JB2NZV4EyZrxyuJp6-AGr_cZdTHWPXpUYKUl4JBXzcV6A5TO-jeltQJZo_Dw4DkhEDd41kjQwy7oD4qVc\" alt=\"\"\/><\/figure>\n\n\n\n<p>So, that\u2019s how we can manipulate different behaviours of our browser.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>headless launch option Let us google search &#8220;upgrade to selenium 4&#8221; Click the first search result link https:\/\/www.selenium.dev\/documentation\/webdriver\/troubleshooting\/upgrade_to_selenium_4\/&nbsp; Notice below that the DesiredCapabilities is now kind of deprecated. It is replaced by browserOptions() classes like FirefoxOptions, ChromeOptions etc Let us see how some of these options work. Launch https:\/\/rahulshettyacademy.com\/ to find out the page title [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1245,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2781","post","type-post","status-publish","format-standard","has-post-thumbnail","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":11,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2781\/revisions"}],"predecessor-version":[{"id":2799,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2781\/revisions\/2799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1245"}],"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}]}}