“NoSuchSessionError: This driver instance does not have a valid session ID” when running a simple Selenium Runner test

和自甴很熟 提交于 2021-01-19 05:26:34

问题


I just installed the latest version of Selenium Runner

npm install -g selenium-side-runner

on my Mac High Sierra. I'm running node v14.1.0. I'm using Chrome Driver v 83. I want to run a very simple file that simply opens a page, waiting for an element on that page to be present. However, I'm getting a "NoSuchSessionError: This driver instance does not have a valid session ID" error.

The Selenium Runner .side file in question is

$ cat selenium/KarmaDecayGetResults.side
{
  "id": "9664bd47-b18f-405f-9bd3-06014919ca7e",
  "version": "2.0",
  "name": "KarmaDecay",
  "url": "http://karmadecay.com",
  "tests": [
    {
      "id": "8f462171-01b8-4247-87b9-40e2d1fef143",
      "name": "KarmaDecay",
      "commands": [
        {
          "id": "c297319b-4350-4f04-b72e-1a347a67100c",
          "comment": "",
          "command": "open",
          "target": "/r/gifs/comments/gz5v5j/caracal_visits_jimmy_kimmel_gets_shy_and_tries_to/",
          "targets": [],
          "value": ""
        },
        {
          "id": "32f35ed7-1a28-4540-a93d-3cb8ba0e012a",
          "comment": "",
          "command": "pause",
          "target": "",
          "targets": [],
          "value": "2000"
        },
        {
          "id": "95261633-22ff-4477-ab6f-7b3354bea8b9",
          "comment": "",
          "command": "setWindowSize",
          "target": "1440x900",
          "targets": [],
          "value": ""
        },
        {
          "id": "fbf35ed7-1a28-4540-a93d-3cb8ba0e012a",
          "comment": "",
          "command": "waitForElementPresent",
          "target": "id=content",
          "targets": [],
          "value": ""
        },
        {
          "id": "aed35ed7-1a28-4540-a93d-3cb8ba0e012a",
          "comment": "",
          "command": "waitForElementVisible",
          "target": "id=content",
          "targets": [],
          "value": ""
        },
        {
          "id": "fbf59ed7-1a28-4540-a93d-3cb8ba0e012a",
          "comment": "",
          "command": "pause",
          "target": "",
          "targets": [],
          "value": "2000"
        }
      ]
    }
  ],
  "suites": [
    {
      "id": "91809d77-24c1-457b-8266-516b2fc58555",
      "name": "Default Suite",
      "persistSession": false,
      "parallel": false,
      "timeout": 300,
      "tests": [
        "8f462171-01b8-4247-87b9-40e2d1fef143"
      ]
    }
  ],
  "urls": [
    "http://karmadecay.com/"
  ],
  "plugins": []
}

Below is the output of running the file ...

$ PATH=/Users/davea/Documents/workspace/article_project/selenium/dev:/usr/local/bin:$PATH /usr/local/bin/selenium-side-runner -c "goog:chromeOptions.args=[--headless,--nogpu] browserName=chrome" selenium/KarmaDecayGetResults.side

 FAIL  ./DefaultSuite.test.js (301.959s)
  Default Suite
    ✕ KarmaDecay (300540ms)

  ● Default Suite › KarmaDecay

    : Timeout - Async callback was not invoked within the 300000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 300000ms timeout specified by jest.setTimeout.Error:

       8 | jest.setTimeout(300000);
       9 | describe("Default Suite", () => {
    > 10 |   it("KarmaDecay", async () => {
         |   ^
      11 |     await tests["KarmaDecay"](driver, vars);
      12 |     expect(true).toBeTruthy();
      13 |   });

      at new Spec (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Suite.<anonymous> (DefaultSuite.test.js:10:3)

  ● Default Suite › KarmaDecay

    NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.

      at ../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/webdriver.js:729:38
      at Object.thenFinally [as finally] (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/promise.js:124:12)
          at runMicrotasks (<anonymous>)
      at WebdriverEnvironment.global.cleanup (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/jest-environment-selenium/dist/index.js:30:7)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        302.159s
Ran all test suites.

Do I need to be doing something different to get my test to run cleanly?


回答1:


invalid session id

The invalid session ID error is a WebDriver error that occurs when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid.

A WebDriver session can be deleted through either of the following ways:

  • Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the quit() method.

  • Implicit session deletion: A WebDriver session is implicitly deleted when you close the last window or tab invoking close().

You can find a detailed discussion in selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python


This usecase

I don't see any such error within your code block. However your main error seems to be...

Timeout - Async callback was not invoked within the 300000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 300000ms timeout specified by jest.setTimeout.Error:

...which implies that the Async callback being referred to in the error is getting timedout even with timeout set as 300000:

jest.setTimeout(300000);

The real issue is with the url of KarmaDecay which contains a hCaptcha to keep off automated bots.

  • URL Snapshot:

hCaptcha_Karmadecay


Conclusion

As per your test setup to execute the selenium-side-runner within the website http://karmadecay.com/ first you have to interact with the captcha to get authenticated and then run your tests.

You can find a couple of relevant discussions on how to interact with recaptcha in:

  • How to identify the 32 bit data-sitekey of ReCaptcha V2 to obtain a valid response programmatically using Selenium and Python Requests?
  • Find the reCAPTCHA element and click on it — Python + Selenium
  • How to click on the reCaptcha using Selenium and Java

Another aspect

If the initial tests works fine but for the rest of your tests you get a session ID error most possibly the Selenium WebDriver controled Browsing Context is getting detected and hence blocking the next requests.

There are different reasons for the WebDriver controled Web Browser to get detected and simultaneously get blocked. You can find a couple of detailed discussion in:

  • Can a website detect when you are using selenium with chromedriver?
  • Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection


来源:https://stackoverflow.com/questions/62285387/nosuchsessionerror-this-driver-instance-does-not-have-a-valid-session-id-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!