Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中带有Selenium的browsermob';不起作用,浏览器会出现不同的错误_Java_Selenium_Browsermob - Fatal编程技术网

JAVA中带有Selenium的browsermob';不起作用,浏览器会出现不同的错误

JAVA中带有Selenium的browsermob';不起作用,浏览器会出现不同的错误,java,selenium,browsermob,Java,Selenium,Browsermob,我在selenium中使用Java,我想使用browsermob:我在不同的浏览器中使用相同的代码,例如firefox: BrowserMobProxyServer proxyServer = new BrowserMobProxyServer(); proxyServer.start(); proxyServer.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());

我在selenium中使用Java,我想使用browsermob:我在不同的浏览器中使用相同的代码,例如firefox:

        BrowserMobProxyServer proxyServer = new BrowserMobProxyServer();
        proxyServer.start();
        proxyServer.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
        proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
        Proxy proxy = ClientUtil.createSeleniumProxy(proxyServer);
        DesiredCapabilities dccFirefox = DesiredCapabilities.firefox();
        dccFirefox.setCapability(CapabilityType.PROXY, proxy);
        this.driver = new FirefoxDriver(dccFirefox);
在Firefox中发生错误:

 org.openqa.selenium.SessionNotCreatedException: InvalidArgumentError: Expected [object Undefined] undefined to be an integer
Build info: version: '3.5.1', revision: '9c21bb67ef', time: '2017-08-17T15:26:08.955Z'
System info: host: 'BEN-PLL-ST1-HP', ip: '192.168.1.9', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_141'
Driver info: driver.version: FirefoxDriver
remote stacktrace: stack backtrace:
   0:           0x4bb74f - <no info>
   1:           0x4bbea9 - <no info>
   2:           0x43ce8d - <no info>
   3:           0x44ce14 - <no info>
   4:           0x44944a - <no info>
   5:           0x4203e1 - <no info>
   6:           0x407dc7 - <no info>
   7:           0x6d95b9 - <no info>
   8:           0x4173a7 - <no info>
   9:           0x6d38b3 - <no info>
  10:         0x775559cd - BaseThreadInitThunk
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$24(ProtocolHandshake.java:360)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Unknown Source)
    at java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.findFirst(Unknown Source)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:363)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:137)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:137)

有人知道为什么会发生这种错误吗?

Geckodriver存在一个公开的问题,导致了这个问题

您可以使用下面的代码来获得解决方法

编辑-1

自geckodriver以来,不受信任的设置已更改。所以更新了代码以适应同样的情况

import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;

/**
 * Created by tarun.lalwani on 5/31/17.
 */
public class TestApp {


    public static void main(String [] args) {

        BrowserMobProxyServer proxyServer = new BrowserMobProxyServer();
        proxyServer.start();
        proxyServer.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
        proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
        Proxy proxy = ClientUtil.createSeleniumProxy(proxyServer);
        FirefoxProfile profile = new FirefoxProfile();
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setAcceptInsecureCerts(true);
        String host = proxy.getHttpProxy().split(":")[0];
        int port = Integer.parseInt(proxy.getHttpProxy().split(":")[1]);

        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("network.proxy.http", host);
        profile.setPreference("network.proxy.http_port", port);
        profile.setPreference("network.proxy.ssl", host);
        profile.setPreference("network.proxy.ssl_port", port);

        profile.setPreference("acceptInsecureCerts", true);
        cap.setCapability(FirefoxDriver.PROFILE, profile);
        FirefoxDriver driver = new FirefoxDriver(cap);

        proxyServer.newHar("mysite");

        driver.get("http://google.com");

        Har har = proxyServer.getHar();
        try {
            har.writeTo(new File("har.json"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver.quit();
    }
}
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test1</groupId>
    <artifactId>test2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <!-- To use the legacy, Jetty-based implementation,
                 change the artifactId to browsermob-core -->
            <artifactId>browsermob-core-littleproxy</artifactId>
            <version>2.1.0-beta-6</version>
            <scope>test</scope>        </dependency>
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <artifactId>browsermob-core-littleproxy</artifactId>
            <version>2.1.0-beta-5</version>
        </dependency>
    </dependencies>
</project>

4.0.0
测试1
测试2
1.0-快照
org.seleniumhq.selenium
硒爪哇
3.4.0
net.lightbody.bmp
browsermob core littleproxy
2.1.0-beta-6
试验
net.lightbody.bmp
browsermob core littleproxy
2.1.0-beta-5

您是否在selenium网格上运行测试?否,仅含黄瓜的selenium webdriver由createSeleniumProxy创建的对象在selenium网格上使用。在没有selenium网格的情况下是否可以使用此库?您使用过哪个版本的firefox?感谢帮助,但现在仍然无法工作。即使我将selenium版本降级为3.4.0,将firefox降级为54.0.1,也会出现错误。也许有什么方法可以为chrome/ie/opera或其他库运行此代码?您正在尝试哪个站点?我使用了FF 54和Selenium 3.4来测试上述代码我也使用了这种组合告诉我该站点,以便我可以check@MateuszSobczak,请检查“立即编辑”。检查过了,它也可以在谷歌上运行
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test1</groupId>
    <artifactId>test2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <!-- To use the legacy, Jetty-based implementation,
                 change the artifactId to browsermob-core -->
            <artifactId>browsermob-core-littleproxy</artifactId>
            <version>2.1.0-beta-6</version>
            <scope>test</scope>        </dependency>
        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <artifactId>browsermob-core-littleproxy</artifactId>
            <version>2.1.0-beta-5</version>
        </dependency>
    </dependencies>
</project>