"NoSuchSessionError:此驱动程序实例没有有效的会话ID"运行简单的Selenium Runner测试时 [英] "NoSuchSessionError: This driver instance does not have a valid session ID" when running a simple Selenium Runner test
问题描述
我刚刚安装了最新版本的Selenium Runner
I just installed the latest version of Selenium Runner
npm install -g selenium-side-runner
在我的Mac High Sierra上.我正在运行节点v14.1.0.我正在使用Chrome驱动程序v83.我想运行一个非常简单的文件,该文件只是打开一个页面,等待该页面上的某个元素出现.但是,我收到"NoSuchSessionError:此驱动程序实例没有有效的会话ID"错误.
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.
有问题的Selenium Runner .side文件是
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?
推荐答案
WebDriver错误.如果会话已被删除或会话ID无效,则会发生这种情况.
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.
可以通过以下两种方式之一删除WebDriver会话:
A WebDriver session can be deleted through either of the following ways:
-
显式会话删除:显式调用
quit()
方法时,将显式删除WebDriver会话.
Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the
quit()
method.
隐式会话删除:关闭最后一个调用close()
的窗口或选项卡时,将隐式删除WebDriver会话.
Implicit session deletion: A WebDriver session is implicitly deleted when you close the last window or tab invoking close()
.
此用例
我在您的代码块中没有看到任何此类错误.但是,您的主要错误似乎是...
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:
...这意味着即使将 timeout 设置为300000
,在错误中引用的Async callback
也会超时:
...which implies that the Async callback
being referred to in the error is getting timedout even with timeout set as 300000
:
jest.setTimeout(300000);
真正的问题是 KarmaDecay 的网址,其中包含一个 hCaptcha 远离自动漫游器.
- URL快照:
根据您的测试设置,在网站 http://karmadecay.com/内执行selenium-side-runner
首先,您必须与验证码进行交互,以进行身份验证然后运行测试.
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.
您可以找到有关如何与 recaptcha 在
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
如果初始测试正常,但在其余测试中,您会得到会话ID 错误,很可能是 WebDriver 控制的浏览上下文被检测到,从而阻止了下一个请求.
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.
WebDriver 控制的 Web浏览器被检测并同时被阻止的原因多种多样.您可以在以下位置找到一些详细的讨论:
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
这篇关于"NoSuchSessionError:此驱动程序实例没有有效的会话ID"运行简单的Selenium Runner测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!