“NoSuchSessionError:此驱动程序实例没有有效的会话 ID"运行简单的 Selenium Runner 测试时 [英] "NoSuchSessionError: This driver instance does not have a valid session ID" when running a simple Selenium Runner test
问题描述
我刚刚安装了最新版本的 Selenium Runner
npm install -g selenium-side-runner
在我的 Mac High Sierra 上.我正在运行节点 v14.1.0.我正在使用 Chrome Driver v 83.我想运行一个非常简单的文件,该文件只是打开一个页面,等待该页面上的元素出现.但是,我收到NoSuchSessionError:此驱动程序实例没有有效的会话 ID"错误.
有问题的 Selenium Runner .side 文件是
$ cat selenium/KarmaDecayGetResults.side{"id": "9664bd47-b18f-405f-9bd3-06014919ca7e","版本": "2.0","name": "KarmaDecay","url": "http://karmadecay.com",测试":[{"id": "8f462171-01b8-4247-87b9-40e2d1fef143","name": "KarmaDecay",命令":[{"id": "c297319b-4350-4f04-b72e-1a347a67100c",评论": "",命令":打开","目标": "/r/gifs/comments/gz5v5j/caracal_visits_jimmy_kimmel_gets_shy_and_tries_to/",目标":[],价值": ""},{"id": "32f35ed7-1a28-4540-a93d-3cb8ba0e012a",评论": "",命令":暂停",目标": "",目标":[],价值":2000"},{"id": "95261633-22ff-4477-ab6f-7b3354bea8b9",评论": "","command": "setWindowSize","目标": "1440x900",目标":[],价值": ""},{"id": "fbf35ed7-1a28-4540-a93d-3cb8ba0e012a",评论": "","command": "waitForElementPresent",目标":id=内容",目标":[],价值": ""},{"id": "aed35ed7-1a28-4540-a93d-3cb8ba0e012a",评论": "","command": "waitForElementVisible",目标":id=内容",目标":[],价值": ""},{"id": "fbf59ed7-1a28-4540-a93d-3cb8ba0e012a",评论": "",命令":暂停",目标": "",目标":[],价值":2000"}]}],套房":[{"id": "91809d77-24c1-457b-8266-516b2fc58555","name": "默认套件",persistSession":假,平行":假,超时":300,测试":[8f462171-01b8-4247-87b9-40e2d1fef143"]}],网址":[http://karmadecay.com/"],插件":[]}
下面是运行文件的输出...
$ 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失败 ./DefaultSuite.test.js (301.959s)默认套件✕ KarmaDecay (300540ms)● 默认套件 › KarmaDecay:超时 - 在 jest.setTimeout.Timeout 指定的 300000 毫秒超时内未调用异步回调 - 在 jest.setTimeout.Error 指定的 300000 毫秒超时内未调用异步回调:8 |jest.setTimeout(300000);9 |描述(默认套件",()=> {>10 |it("KarmaDecay", async () => {|^11 |等待测试[KarmaDecay"](驱动程序,变量);12 |期望(真).toBeTruthy();13 |});在新规范 (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)<匿名>(DefaultSuite.test.js:10:3)● 默认套件 › KarmaDecayNoSuchSessionError:此驱动程序实例没有有效的会话 ID(您是否调用了 WebDriver.quit()?)并且可能不再使用.在 ../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/webdriver.js:729:38在 Object.thenFinally [as finally] (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/promise.js:124:12)在运行微任务(<匿名>)在 WebdriverEnvironment.global.cleanup (../../../../../../usr/local/lib/node_modules/selenium-side-runner/node_modules/jest-environment-selenium/dist/index.js:30:7)测试套件:1 个失败,总共 1 个测试:1 次失败,总共 1 次快照:共 0 个时间:302.159s运行所有测试套件.
我是否需要做一些不同的事情才能让我的测试干净利落地运行?
首先,您必须与 captcha 进行身份验证,然后运行您的测试.
您可以找到一些关于如何与 重新验证码:
- 如何使用 Selenium 和 Python 请求以编程方式识别 ReCaptcha V2 的 32 位数据站点密钥以获取有效响应?
- 找到 reCAPTCHA 元素并点击它——Python + Selenium
- 如何点击使用 Selenium 和 Java 的 reCaptcha
另一方面
如果初始测试工作正常,但对于其余测试,您会收到会话 ID 错误,很可能是 Selenium WebDriver 控制的浏览上下文被检测到,因此阻止了下一个请求.
WebDriver 控制的 Web 浏览器被检测并同时被阻止的原因有多种.您可以在以下位置找到一些详细的讨论:
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?
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:
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
这篇关于“NoSuchSessionError:此驱动程序实例没有有效的会话 ID"运行简单的 Selenium Runner 测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!