gpt4 book ai didi

java - Python - 无法使用 htmlunitdriver 创建与 Selenium 独立服务器的 session

转载 作者:行者123 更新时间:2023-12-02 09:55:32 24 4
gpt4 key购买 nike

我正在尝试使用带有 python 脚本的 htmlUnit 驱动程序的 selenium 远程独立服务器。
首先,我使用 htmlUnit 驱动程序启动独立服务器

java -cp htmlunit-driver-2.35.1-jar-with-dependencies.jar -jar selenium-server-standalone-3.141.0.jar

09:28:31.731 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.0, revision: 2ecb7d9a 09:28:31.810 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444 2019-05-08 09:28:31.857:INFO::main: Logging initialized @317ms to org.seleniumhq.jetty9.util.log.StdErrLog 09:28:32.059 INFO [WebDriverServlet.] - Initialising WebDriverServlet 09:28:32.543 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

之后我尝试在我的 pyhton 脚本中使用 htmlunit 驱动程序

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities={'browserName': 'htmlunit', 'version': '2','javascriptEnabled': True})
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

但我收到此错误

selenium.common.exceptions.SessionNotCreatedException: Message: Unable to create session from { "desiredCapabilities": { "browserName": "htmlunit", "javascriptEnabled": true, "version": "2" }, "capabilities": { "firstMatch": [ { "browserName": "htmlunit" }, { "browserName": "htmlunit", "browserVersion": "2" } ] } } Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:22:52' System info: host: 'DESKTOP-xxxx', ip: 'xxx.xxx.xxx.xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_212' Driver info: driver.version: unknown Stacktrace: at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$null$4 (NewSessionPipeline.java:76) at java.util.Optional.orElseThrow (None:-1) at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$createNewSession$5 (NewSessionPipeline.java:75) at java.util.Optional.orElseGet (None:-1) at org.openqa.selenium.remote.server.NewSessionPipeline.createNewSession (NewSessionPipeline.java:73) at org.openqa.selenium.remote.server.commandhandler.BeginSession.execute (BeginSession.java:65) at org.openqa.selenium.remote.server.WebDriverServlet.lambda$handle$0 (WebDriverServlet.java:235) at java.util.concurrent.Executors$RunnableAdapter.call (None:-1) at java.util.concurrent.FutureTask.run (None:-1) at java.util.concurrent.ThreadPoolExecutor.runWorker (None:-1) at java.util.concurrent.ThreadPoolExecutor$Worker.run (None:-1) at java.lang.Thread.run (None:-1)

我似乎正确地遵循了文档,但我不明白错误可能出在哪里。
你能告诉我如何解决这个问题吗?

谢谢

最佳答案

根据 java command line documentation:

When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored.

因此您需要修改用于启动 Selenium Grid 的命令看起来像:

java -cp "htmlunit-driver-2.35.1-jar-with-dependencies.jar;selenium-server-standalone-3.141.59.jar" org.openqa.grid.selenium.GridLauncherV3

我还建议更改此行:

driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities={'browserName': 'htmlunit', 'version': '2','javascriptEnabled': True})

对于这个:

driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS)

这样您的脚本就有更好的机会在未来的库升级中生存

关于java - Python - 无法使用 htmlunitdriver 创建与 Selenium 独立服务器的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56035960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com