Extract session_id and _url from driver object
We are taking advantage of the fact that Selenium lets you attach a driver to a session id and a URL. You can extract your session id and _url from the driver object as mentioned below:
session_url = driver.command_executor._url
session_id = driver.session_id
Tie it up with your framework
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
def get_element(self,xpath):
"Return the DOM element of the xpath or 'None' if the element is not found "
dom_element = None
try:
dom_element = self.driver.find_element(xpath)
except Exception,e:
self.write(str(e),'debug')
self.write("Check your locator-'%s"%xpath)
#Print the session_id and _url in case the element is not found
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))
return dom_element
Use the session id and URL
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:
from selenium import webdriver
#Pass the session_url you extracted
driver = webdriver.Remote(command_executor=session_url,desired_capabilities={})
#Attach to the session id you extracted
driver.session_id = session_id
#Resume from that browser state
elm = driver.find_element_by_xpath("xpath")
elm.click() #Perform required action
Hope this helps!
If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training!