问题
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)?
回答1:
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());
回答2:
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:
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.
来源:https://stackoverflow.com/questions/36476111/is-it-possible-to-get-session-id-of-active-driver-opened-with-selenium-and-appiu