简体   繁体   中英

org.openqa.selenium.SessionNotCreatedException: Message: Could not start a new session. Response code 500 error using Appium Java

I am still very new to UI test automation. When I run the code up to line 27 ("String session ID..."), my APK starts on the emulator and my Appium server also runs smoothly. Now when I want to run a Simple UI test, I get the following error message:

 Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 404. Message: The session identified by 72bf293f-5423-4430-b126-96be60d37a2c is not known

Code:

package setup;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class CapabilityBase {

    public static void main(String[] args) throws MalformedURLException {

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"android");
        desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "12");
        desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Pixel");
        desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
        desiredCapabilities.setCapability("autoGrantPermissions", true);
        desiredCapabilities.setCapability(MobileCapabilityType.APP, "/Users/firstname.lastname/eclipse-workspace/BMUI_testAutomation/src/test/resources/SystemUnderTest/app-staging.apk");

        URL url = new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver = new AndroidDriver(url, desiredCapabilities);
        String sessionId = driver.getSessionId().toString();

        //driver.findElement(By.id("com.google.android.permissioncontroller/com.android.permissioncontroller.permission.ui.GrantPermissionsActivity/permission_allow_button")).click();

        driver.findElement (new By.ByXPath("//android.widget.TextView[contains(@text,'Your Login']")).click();
        //driver.findElement (new By.ByXPath("//android.widget.TextView[contains(@text,'Your Login']")).sendKeys("02209");
        //driver.findElement (new By.ByXPath("//android.widget.TextView[contains(@text,'Your PIN']")).click();
        //driver.findElement (new By.ByXPath("//android.widget.TextView[contains(@text,'Your PIN']")).sendKeys("030378");

// System.out.print("test passed");


    }
}

Error:


    Executing ':CapabilityBase.main()'...
    
    > Task :compileJava NO-SOURCE
    > Task :processResources NO-SOURCE
    > Task :classes UP-TO-DATE
    > Task :compileTestJava UP-TO-DATE
    > Task :processTestResources UP-TO-DATE
    > Task :testClasses UP-TO-DATE
    
    > Task :CapabilityBase.main()
    Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 404. Message: The session identified by 72bf293f-5423-4430-b126-96be60d37a2c is not known 
    Host info: host: 'MBPvonEnrico2', ip: '2a02:8109:9f80:1a68:0:0:0:ea6f%en0'
    Build info: version: '4.7.1', revision: 'c6795baf1a3'
    System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.16', java.version: '1.8.0_292'
    Driver info: io.appium.java_client.android.AndroidDriver
    Command: [null, newSession {capabilities=[{appium:app=/Users/enrico.weck/eclipse-workspace/BMUI_testAutomation/src/test/resources/SystemUnderTest/app-staging.apk, appium:autoGrantPermissions=true, appium:automationName=uiautomator2, appium:deviceName=Pixel, platformName=ANDROID, appium:platformVersion=12}], desiredCapabilities=Capabilities {app: /Users/enrico.weck/eclipse-..., autoGrantPermissions: true, automationName: uiautomator2, deviceName: Pixel, platformName: ANDROID, platformVersion: 12}}]
    Capabilities {}
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:133)
        at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:102)
        at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:194)
        at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:262)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:551)
        at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:229)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159)
        at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:80)
        at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:92)
        at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:117)
        at setup.CapabilityBase.main(CapabilityBase.java:26)
    
    > Task :CapabilityBase.main() FAILED
    
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
    
    You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
    
    See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings
    3 actionable tasks: 1 executed, 2 up-to-date
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':CapabilityBase.main()'.
    > Process 'command '/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
    
    * Try:
    > Run with --stacktrace option to get the stack trace.
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 27s
    ```

Server Logs:

    
        [HTTP] <-- POST /wd/hub/session 200 29274 ms - 1106
        [HTTP] 
        [HTTP] --> POST /wd/hub/session/8e481bd0-d86d-4310-aac1-d0dcebe57e76/element
        [HTTP] {"using":"xpath","value":"//android.widget.TextView[contains(@text,'Your Login']"}
        [W3C (8e481bd0)] Calling AppiumDriver.findElement() with args: ["xpath","//android.widget.TextView[contains(@text,'Your Login']","8e481bd0-d86d-4310-aac1-d0dcebe57e76"]
        [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator
        [BaseDriver] Waiting up to 0 ms for condition
        [WD Proxy] Matched '/element' to command name 'findElement'
        [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8202/wd/hub/session/0a905b04-4358-45e9-ba42-c3e8ec736cee/element] with body: {"strategy":"xpath","selector":"//android.widget.TextView[contains(@text,'Your Login']","context":"","multiple":false}
        [WD Proxy] Got response with status 400: {"sessionId":"0a905b04-4358-45e9-ba42-c3e8ec736cee","value":{"error":"invalid selector","message":"org.eclipse.wst.xml.xpath2.processor.XPathParserException: CUP parser error: Parser error: line 1 col 54: ]","stacktrace":"io.appium.uiautomator2.common.exceptions.InvalidSelectorException: org.eclipse.wst.xml.xpath2.processor.XPathParserException: CUP parser error: Parser error: line 1 col 54: ]\n\tat io.appium.uiautomator2.core.AccessibilityNodeInfoDumper.findNodes(AccessibilityNodeInfoDumper.java:213)\n\tat io.appium.uiautomator2.utils.ElementLocationHelpers.getXPathNodeMatch(ElementLocationHelpers.java:119)\n\tat io.appium.uiautomator2.handler.FindElement.findElement(FindElement.java:88)\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:67)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:266)\n\tat io.appium.uiautomator2.server.AppiumServlet.handl...
        [W3C] Matched W3C error code 'invalid selector' to InvalidSelectorError
        [W3C (8e481bd0)] Encountered internal error running command: io.appium.uiautomator2.common.exceptions.InvalidSelectorException: org.eclipse.wst.xml.xpath2.processor.XPathParserException: CUP parser error: Parser error: line 1 col 54: ]
        [W3C (8e481bd0)]    at io.appium.uiautomator2.core.AccessibilityNodeInfoDumper.findNodes(AccessibilityNodeInfoDumper.java:213)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.utils.ElementLocationHelpers.getXPathNodeMatch(ElementLocationHelpers.java:119)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.handler.FindElement.findElement(FindElement.java:88)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:67)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:266)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:260)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
        [W3C (8e481bd0)]    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
        [W3C (8e481bd0)]    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435)
        [W3C (8e481bd0)]    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
        [W3C (8e481bd0)]    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
        [W3C (8e481bd0)]    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
        [W3C (8e481bd0)]    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
        [W3C (8e481bd0)]    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
        [W3C (8e481bd0)]    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
        [W3C (8e481bd0)]    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911)
        [W3C (8e481bd0)]    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
        [W3C (8e481bd0)]    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611)
        [W3C (8e481bd0)]    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552)
        [W3C (8e481bd0)]    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466)
        [W3C (8e481bd0)]    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438)
        [W3C (8e481bd0)]    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140)
        [W3C (8e481bd0)]    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
        [W3C (8e481bd0)]    at java.lang.Thread.run(Thread.java:920)
        [W3C (8e481bd0)] Caused by: org.eclipse.wst.xml.xpath2.processor.XPathParserException: CUP parser error: Parser error: line 1 col 54: ]
        [W3C (8e481bd0)]    at org.eclipse.wst.xml.xpath2.processor.internal.InternalXPathParser.parse(InternalXPathParser.java:67)
        [W3C (8e481bd0)]    at org.eclipse.wst.xml.xpath2.processor.JFlexCupParser.parse(JFlexCupParser.java:33)
        [W3C (8e481bd0)]    at org.eclipse.wst.xml.xpath2.processor.Engine.parseExpression(Engine.java:17)
        [W3C (8e481bd0)]    at io.appium.uiautomator2.core.AccessibilityNodeInfoDumper.findNodes(AccessibilityNodeInfoDumper.java:211)
        [W3C (8e481bd0)]    ... 37 more
        [W3C (8e481bd0)] Caused by: org.eclipse.wst.xml.xpath2.processor.internal.CupError
        [W3C (8e481bd0)]    at org.eclipse.wst.xml.xpath2.processor.internal.XPathCup.report_error(XPathCup.java:3238)
        [W3C (8e481bd0)]    at java_cup.runtime.lr_parser.syntax_error(lr_parser.java:396)
        [W3C (8e481bd0)]    at java_cup.runtime.lr_parser.parse(lr_parser.java:595)
        [W3C (8e481bd0)]    at org.eclipse.wst.xml.xpath2.processor.internal.InternalXPathParser.parse(InternalXPathParser.java:47)
        [W3C (8e481bd0)]    ... 40 more
        [W3C (8e481bd0)] 
        [HTTP] <-- POST /wd/hub/session/8e481bd0-d86d-4310-aac1-d0dcebe57e76/element 400 1414 ms - 5068


I have already tried a few things as well as a JDK or appium upgrade or changing the runner to intellij. Maybe someone can help me thx.

Sounds like the way you are using the selector is not correct.

Please change this statement:

driver.findElement (new By.ByXPath("//android.widget.TextView[contains(@text,'Your Login']")).click();

To this,

driver.findElement (AppiumBy.xpath("//android.widget.TextView[contains(@text,'Your Login']")).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM