如何在Selenium中启用PhantomJS WebDriver?[英] How to enable PhantomJS WebDriver in Selenium?

问题描述

我需要将我的 Java 程序从 ChromeDriver 重写为 PhantomJS.我想我应该只启用 PhantomJS 而不是 ChromeDriver,仅此而已(对吗?).我尝试了几种方法来做到这一点,但我总是得到 NoClassDefFoundError.

我启用 ChromeDriver 的方式:

System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_PATH);
WebDriver driver = new ChromeDriver();

这就是我尝试启用 PhantomJS 的方式:

 DesiredCapabilities DesireCaps = new DesiredCapabilities();
 DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
 WebDriver driver = new PhantomJSDriver(DesireCaps);

第二次尝试

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJSDRIVER_PATH);
WebDriver driver = new PhantomJSDriver();

第三

File src = new File(PHANTOMJSDRIVER_PATH);
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();

推荐答案

我在我的项目中使用了如下,它可以工作.

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability("phantomjs.binary.path","path/to/phantomjsdriver");
driver = new PhantomJSDriver(capabilities);

另外,请确保您的项目中有 phantomjs 依赖项.

<dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.3.0</version>
</dependency>

本文地址:https://www.itbaoku.cn/post/1740101.html