{"id":5537,"date":"2017-03-24T07:45:09","date_gmt":"2017-03-24T11:45:09","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=5537"},"modified":"2018-04-03T10:14:32","modified_gmt":"2018-04-03T14:14:32","slug":"reuse-existing-selenium-browser-session","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/reuse-existing-selenium-browser-session\/","title":{"rendered":"How to reuse existing Selenium browser session"},"content":{"rendered":"<p>Suppose you are running a long Selenium test. The test runs fine but fails towards the end as some element was not found or was not clickable. Ideally, you would like to fix the locator and check it immediately. Now you wonder if you can reuse your browser session from the same point where the failure occurred. This post shows you how to do just that! This would help you to debug the locators used quickly, instead of running your whole test again. <\/p>\n<hr>\n<h3>Extract session_id and _url from driver object<\/h3>\n<p>We are taking advantage of the fact that Selenium lets you attach a driver to a session id and an URL. You can extract your session id and _url from the driver object as mentioned below<\/p>\n<pre lang=\"python\">\r\nsession_url = driver.command_executor._url  \r\nsession_id = driver.session_id  \r\n<\/pre>\n<hr>\n<h3>Tie it up with your framework<\/h3>\n<p>In case you are using a framework, you can print out the session id and url in the method where you try to get the element. In an example shown below, I am using a get_element method which would return the DOM element. I am printing the session id and url whenever the find_element method fails<\/p>\n<pre lang=\"python\">\r\ndef get_element(self,xpath):\r\n        \"Return the DOM element of the xpath or 'None' if the element is not found \"\r\n        dom_element = None\r\n        try:\r\n            dom_element = self.driver.find_element(xpath)\r\n        except Exception,e:\r\n            self.write(str(e),'debug')\r\n            self.write(\"Check your locator-'%s\"%xpath)\r\n            #Print the session_id and _url in case the element is not found\r\n            self.write(\"In case you want to reuse session, the session_id and _url for current browser session are: %s,%s\"%(driver.session_id ,driver.command_executor._url))\r\n\r\n        return dom_element\r\n<\/pre>\n<hr>\n<h3>Use the session id and url<\/h3>\n<p>Now you can use the session id and url, in order to debug the locators. Open python command line tool and use the below code<\/p>\n<pre lang=\"python\">\r\n\r\nfrom selenium import webdriver\r\n\r\n#Pass the session_url you extracted\r\ndriver = webdriver.Remote(command_executor=session_url,desired_capabilities={})\r\n\r\n#Attach to the session id you extracted\r\ndriver.session_id = session_id\r\n\r\n#Resume from that browser state\r\nelm = driver.find_element_by_xpath(\"xpath\")\r\n\r\nelm.click() #Perform required action\r\n<\/pre>\n<hr>\n<p><strong>Note1: <\/strong> Currently I am able to reuse the session extracted only from Chrome browser. For Firefox there are some Issue. <a href=\"https:\/\/github.com\/mozilla\/geckodriver\/issues\/104\">Link 1<\/a> and <a href=\"http:\/\/stackoverflow.com\/questions\/37963785\/cannot-attach-to-an-existing-selenium-session-via-geckodriver\/37968826#37968826\">Link 2<\/a><\/p>\n<p><strong>Note2: <\/strong> This code is present in our open-sourced <a href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">Python-Selenium test automation framework<\/a> based on the page object pattern. We strongly recommend you check it out!<\/p>\n<p><strong>If you are a startup finding it hard to hire technical QA engineers, learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2 Services<\/a>.<\/strong><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you are running a long Selenium test. The test runs fine but fails towards the end as some element was not found or was not clickable. Ideally, you would like to fix the locator and check it immediately. Now you wonder if you can reuse your browser session from the same point where the failure occurred. This post shows [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-5537","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/5537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=5537"}],"version-history":[{"count":13,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/5537\/revisions"}],"predecessor-version":[{"id":9469,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/5537\/revisions\/9469"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=5537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=5537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=5537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}