我是 phantomjs 驱动程序的新手,我需要使用 phantomjs headless 驱动程序在后台运行我的脚本。
这是我的代码,我收到空指针异常。
目前正在使用selenium 2.32,testNG,phantomjs jar 1.0.3
public class PhantomjsDemo {
public WebDriver driver;
@BeforeMethod
public void setup(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.2-windows\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get("www.google.com");
}
@Test
public void google(){
driver.findElement(By.xpath("//*[@id='gbqfba']")).getText();
driver.findElement(By.xpath("//*[@id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[@id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[@id='gbqfba']")).click();
}
@AfterMethod
public void close(){
driver.quit();
}
}
最佳答案
您不是在 setup() 方法中初始化 Webdriver
成员变量,而是初始化一个方法变量:
WebDriver driver = new PhantomJSDriver(caps);
更改为
this.driver = new PhantomJSDriver(caps);
NPE 应该消失。
关于java - 无法使用 phantomjs 驱动程序运行 selenium 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19157924/