Is it possible to get session_id of active driver

2019-02-14 20:38发布

While automating I open several browsers, say Firefox, with

driver1 = webdriver.Firefox()
driver2 = webdriver.Firefox()
driver3 = webdriver.Firefox()
.....

Is there a way to get the session_id and webdriver itself of the active Browser? The same question for Appium. Is it possible to get session_id and driver itself of the active device (virtual or real)?

3条回答
做个烂人
2楼-- · 2019-02-14 21:18

There is a workaround for the problem. You could create a Session. This gives you the Webdriver Instance, but also the sessionID.

DefaultDriverFactory defaultDriverFactory = new DefaultDriverFactory(Platform.WINDOWS); 
TemporaryFilesystem temporaryFilesystem = TemporaryFilesystem.getDefaultTmpFS(); 
ChromeOptions chromeOptions = new ChromeOptions();
Session session = DefaultSession.createSession(defaultDriverFactory, temporaryFilesystem, chromeOptions); 
WebDriver webDriver = session.getDriver();
SessionId sessionId = session.getSessionId(); 
查看更多
对你真心纯属浪费
3楼-- · 2019-02-14 21:28

Use DataFactory. The following snippet (written in Katalon Studio, but using selenium, so I guess it would be similar or same in other tools)

WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new ChromeDriver()

DriverFactory.changeWebDriver(driver1)
driver1.get("https://www.example.com")
println DriverFactory.webDriver

DriverFactory.changeWebDriver(driver2)
driver2.get("https://news.example.com")
println DriverFactory.webDriver

will print out to console:

ChromeDriver: chrome on XP (fc70e83ced12b3e9beed990e88670d8e)
ChromeDriver: chrome on XP (a810d0cf94dbaf1cbd018542f9c983c3)

with session ID in brackets.

查看更多
Fickle 薄情
4楼-- · 2019-02-14 21:33

To get the driver session id with Selenium / Java:

WebDriver driver = new FirefoxDriver();

SessionId session = ((FirefoxDriver)driver).getSessionId();
System.out.println("Session id: " + session.toString());

To get the remote driver session id with Selenium / Java:

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4722/wd/hub"), capabilities);

SessionId session = ((RemoteWebDriver)driver).getSessionId();
System.out.println("Session id: " + session.toString());
查看更多
登录 后发表回答