简体   繁体   English

使用 htmlunit 驱动程序设置 python

[英]set up python with htmlunit driver

i am trying to set up python with selenium htmlunit driver on ubuntu 18.04.我正在尝试在 ubuntu 18.04 上使用 selenium htmlunit 驱动程序设置 python。 launching selenium standalone server启动 selenium 独立服务器

java -jar selenium-server-standalone-3.141.59.jar

output 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.我浏览了互联网,发现设置所需功能的不同变体,例如 {'platform': None, ' desired_capabilities {'platform': None, 'browserName': 'htmlunit', 'version': None}desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS并像这样启动 selenium 服务器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.我会很感激你的帮助。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM