问题
i am trying to set up python with selenium htmlunit driver on ubuntu 18.04. launching selenium standalone server
java -jar selenium-server-standalone-3.141.59.jar
output
00:00:46.177 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision:e82be7d358
00:00:46.299 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444
2020-05-25 00:00:46.366:INFO::main: Logging initialized @483ms to org.seleniumhq.jetty9.util.log.StdErrLog
00:00:46.685 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
00:00:46.811 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
then in my code i am trying connect to it like this
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT
)
and getting error
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to create session from {
"desiredCapabilities": {
"browserName": "htmlunit",
"version": null,
"platform": null
},
"capabilities": {
"firstMatch": [
{
"browserName": "htmlunit"
}
]
}
}
Driver info: driver.version: unknown
Stacktrace:
at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$null$4 (NewSessionPipeline.java:76)
at java.util.Optional.orElseThrow (Optional.java:290)
at org.openqa.selenium.remote.server.NewSessionPipeline.lambda$createNewSession$5 (NewSessionPipeline.java:75)
at java.util.Optional.orElseGet (Optional.java:267)
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 (Executors.java:511)
at java.util.concurrent.FutureTask.run (FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run (Thread.java:748)
i've looked across internet and found different variations of setting desired_capabilities
like
{'platform': None, 'browserName': 'htmlunit', 'version': None}
or
desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS
and launching selenium server like this java -cp selenium-server-standalone-3.141.59.jar:htmlunit-driver-2.40.9-jar-with-dependencies.jar org.openqa.grid.selenium.GridLauncherV3
but this didnt help and the error is the same.
I will be grateful for your help.
回答1:
try this:
import subprocess
from subprocess import PIPE, Popen
from selenium import webdriver
pid=Popen(['java','-cp','.\headless\htmlunit-driver-2.45.0-jar-with-dependencies.jar;.\headless\selenium-server-standalone-3.141.59.jar','org.openqa.grid.selenium.GridLauncherV3']).pid
print(pid)
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT) #WITHJS)
driver.get('https://www.stackoverflow.com/')
print(driver.title)
print(driver.current_url)
来源:https://stackoverflow.com/questions/61992621/set-up-python-with-htmlunit-driver