【问题标题】:java.lang.Long cannot be cast to class java.lang.Integer when setting proxy version (using an int) in Selenium?在 Selenium 中设置代理版本(使用 int)时,java.lang.Long 不能转换为类 java.lang.Integer?
【发布时间】:2021-02-20 05:47:31
【问题描述】:

我尝试在 Selenium 中为 chromedriver 设置 socks 代理。代码很基础:

System.setProperty("webdriver.chrome.driver", "/opt/local/bin/chromedriver"); 
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setSocksProxy("127.0.0.1:9050");
proxy.setSocksVersion(5);
options.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(options);

执行时会崩溃

Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)

在 myClass.MyClass.main(MyClass.java:32)

原因:java.lang.ClassCastException:类 java.lang.Long 不能转换为类 java.lang.Integer(java.lang.Long 和 java.lang.Integer 在模块 java.base 中加载程序“引导程序”)

    at org.openqa.selenium.Proxy.<init>(Proxy.java:94)
    at org.openqa.selenium.remote.ProtocolHandshake$Result.lambda$static$0(ProtocolHandshake.java:151)
    at org.openqa.selenium.remote.ProtocolHandshake$Result.<init>(ProtocolHandshake.java:166)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$successHandler$1(W3CHandshakeResponse.java:107)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    ... 6 more

第 32 行是WebDriver driver = new ChromeDriver(options);

我很困惑。我尝试从5 中显式创建一个整数,但结果是一样的。

当我阅读堆栈跟踪时,感觉就像 Selenium 本身在沿途某处将我的 int 转换为 long !?!? setSocksVersion 需要一个 int 为什么突然变长了?

  1. 如何在 Selenium 中设置 socks5-proxy?我的做法对吗?

  2. 为什么5会突然转成long,怎么办?

【问题讨论】:

    标签: java selenium stack-trace


    【解决方案1】:

    看起来像一个已知的错误

    试试

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--proxy-server=socks5://" + host + ":" + port);
    WebDriver driver = new ChromeDriver(options);
    

    取自https://github.com/SeleniumHQ/selenium/issues/5299

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2019-12-19
      • 1970-01-01
      • 2016-07-26
      • 2013-08-24
      • 2015-05-29
      相关资源
      最近更新 更多