Java Appium 错误:无法创建新会话。(原始错误:没有从 Chromedriver 获得会话重定向)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24626529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:32:58  来源:igfitidea点击:

Appium Error : A new session could not be created. (Original error: Did not get session redirect from Chromedriver)

javaseleniumappium

提问by Pon

I am trying to run my first test through Appium and getting the below error.

我正在尝试通过 Appium 运行我的第一个测试并收到以下错误。

org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Did not get session redirect from Chromedriver) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 4.64 seconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_51'
Driver info: org.openqa.selenium.remote.RemoteWebDriver

Appium Logs:

Appium 日志:

ERROR: debug: Appium request initiated at /wd/hub/session
info: Looks like we want chrome on android
info: Creating new appium session 99ff77a0-86fa-4851-93ad-5375c95656e7
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" devices
info: Preparing device for session
info: Not checking whether app is present since we are assuming it's already on the device
info: Checking whether adb is present
info: [ADB] Using adb from D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe
info: Trying to find a connected android device
info: [ADB] Getting connected devices...
info: [ADB] 1 device(s) connected
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" -s emulator-5554 wait-for-device
info: Setting device id to emulator-5554
info: [ADB] Waiting for device to be ready and to respond to shell commands (timeout = 5)
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" -s emulator-5554 shell "echo 'ready'"
info: Starting logcat capture
ERROR: debug: Pushing unlock helper app to device...
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" -s emulator-5554 install "C:\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" -s emulator-5554 shell "dumpsys window"
info: Writing dumpsys output to C:\Appium\node_modules\appium\.dumpsys.log
ERROR: debug: Screen already unlocked, continuing.
info: Creating Chrome session
info: Ensuring Chromedriver exists
info: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %b))
info: No old chromedrivers seemed to exist
info: Spawning chromedriver with: C:\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
info: [CHROMEDRIVER] Starting ChromeDriver (v2.9.248315) on port 9515
info: Making http request with opts: {"url":"http://****:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.android.browser","androidActivity":"com.android.browser.BrowserActivity","androidDeviceSerial":"emulator-5554"}}}}
ERROR: debug: executing: "D:\****\adt-bundle-windows-x86-20140321\sdk\platform-tools\adb.exe" -s emulator-5554 devices
info: [CHROMEDRIVER STDERR] [0.003][SEVERE]: Could not bind socket to 0.0.0.0:9515
info: [CHROMEDRIVER] Port not available. Exiting...
info: Chromedriver exited with code 1
info: [ADB] Getting connected devices...
ERROR: error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"ba9a494082776d6b4836da850072cbcb","status":13,"value":{"message":"unknown error: Device emulator-5554 is already in use\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)"}}
info: Cleaning up appium session
ERROR: error: Failed to start an Appium session, err was: Error: Did not get session redirect from Chromedriver
info: Error: Did not get session redirect from Chromedriver
at null. (C:\Appium\node_modules\appium\lib\devices\android\chromedriver.js:195:12)
at Request._callback (C:\Appium\node_modules\appium\lib\devices\common.js:104:5)
at Request.self.callback (C:\Appium\node_modules\appium\node_modules\request\request.js:121:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request. (C:\Appium\node_modules\appium\node_modules\request\request.js:985:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage. (C:\Appium\node_modules\appium\node_modules\request\request.js:936:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
info: Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Did not get session redirect from Chromedriver)","origValue":"Did not get session redirect from Chromedriver"},"sessionId":null}

I want to use only Android default Browser and not Chrome. I dont know why Appium is trying to launch Chromedriver and Chrome Browser. Below is the code i used.

我只想使用 Android 默认浏览器而不是 Chrome。我不知道为什么 Appium 试图启动 Chromedriver 和 Chrome 浏览器。下面是我使用的代码。

import java.net.URL;                

import org.openqa.selenium.WebDriver;                
import org.openqa.selenium.remote.CapabilityType;                
import org.openqa.selenium.remote.DesiredCapabilities;                
import org.openqa.selenium.remote.RemoteWebDriver;                
import org.testng.annotations.AfterMethod;                
import org.testng.annotations.BeforeMethod;                
import org.testng.annotations.Test;                

public class Appium {                

    public WebDriver driver;            

    @BeforeMethod            
    public void beforeMethod() throws Exception {            
        // set up appium        
        DesiredCapabilities capabilities = new DesiredCapabilities();        
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "Browser");        
        capabilities.setCapability("platformName", "Android");        
        capabilities.setCapability("deviceName", "Android Emulator");        
        capabilities.setCapability("platformVersion", "4.4.2");        
        WebDriver driver = new RemoteWebDriver(new URL(        
                http://****:4723/wd/hub), capabilities);
        driver.get("http://www.yahoo.com");        
    }            

    @Test            
    public void f() {            

    }            

    @AfterMethod            
    public void afterMethod() {            
        driver.quit();        
    }            

}

回答by Nguyen Vu Hoang

The safest way to set for android capabilities = DesiredCapabilities.android()

设置 android 功能的最安全方法 = DesiredCapabilities.android()

PS: You got session is not created because your script is not able to connect to selenium node/hub; you should check your configuration

PS:您的会话未创建,因为您的脚本无法连接到 selenium 节点/集线器;你应该检查你的配置

As in your post stacktrace, it connects to http://**:9515/ (not like in your mentioned port 4723)

就像在您的堆栈跟踪后一样,它连接到 http://**:9515/(不像您提到的端口 4723)

回答by Jess

You need to set the below URL to http://0.0.0.0:9515/wd/hubinstead of ****:4723...

您需要将以下 URL 设置为http://0.0.0.0:9515/wd/hub而不是****:4723...

WebDriver driver = new RemoteWebDriver(new URL(http://****:4723/wd/hub), capabilities);

WebDriver driver = new RemoteWebDriver(new URL(http://****:4723/wd/hub), capabilities);

Chromedriver's port defaults to 9515, not 4723. You should read the documentation on this. There are other capabilities that need to be set correctly(like BROWSER_NAME)

Chromedriver 的端口默认为 9515,而不是 4723。您应该阅读有关此的文档。还有其他功能需要正确设置(例如BROWSER_NAME

回答by Naman

Though this issue is quite an old one and a lot of definition has changed on appium client usage along with the chrome driver version updates as well. Yet to what I could figure out was that the issue here was this from the logs :

尽管这个问题已经很老了,而且很多关于 appium 客户端使用的定义以及 chrome 驱动程序版本更新也发生了变化。然而,我能弄清楚的是,这里的问题来自日志:

ERROR: error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"ba9a494082776d6b4836da850072cbcb","status":13,"value":{"message":"unknown error: Device emulator-5554 is already in use\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)"}}

错误:错误:Chromedriver 创建会话无效。状态为 200,正文为 {"sessionId":"ba9a494082776d6b4836da850072cbcb","status":13,"value":{"message":"unknown error: Device emulator-5554 is already in use\n(驱动程序信息:chromedriver= 2.9.248315,平台=Windows NT 6.1 SP1 x86)"}}

which resulted further into :

这进一步导致:

org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Did not get session redirect from Chromedriver) (WARNING: The server did not provide any stacktrace information)

org.openqa.selenium.SessionNotCreatedException:无法创建新会话。(原始错误:没有从 Chromedriver 获得会话重定向)(警告:服务器没有提供任何堆栈跟踪信息)

and hence SessionNotCreatedException.

因此SessionNotCreatedException

Simpler fix to this could be ensuring no other session is in progress before initiating another to avoid conflicts of ports and session failure.

对此的更简单修复可能是确保在启动另一个会话之前没有其他会话正在进行中,以避免端口冲突和会话失败。

回答by Gaurav Lad

Try running after commenting below statement

在评论下面的语句后尝试运行

 capabilities.setCapability("platformVersion", "4.4.2"); 

回答by Karthik

Replace old chrome driver to new chrome driver in appium location, it worked for me.

在 appium 位置将旧的 chrome 驱动程序替换为新的 chrome 驱动程序,它对我有用。

The Appium location should be something like:

Appium 的位置应该是这样的:

C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\

Appium version : 1.4.16.1

Appium 版本:1.4.16.1