How to implement PhantomJS with Selenium WebDriver using java

和自甴很熟 提交于 2019-11-28 19:53:50
Sankar

Try this, it worked for me

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);                
caps.setCapability("takesScreenshot", true);  
caps.setCapability(
                        PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                        "your custom path\\phantomjs.exe"
                    );
WebDriver driver = new  PhantomJSDriver(caps);

then rest of them are similar..and pls kindly comment your observation ,have a great day :)

I am facing the same problem with you. I can use the same code with FirefoxDriver, but when I change it to PhantomJSDriver, it broke down.

Can you try the FirefoxDriver instead of PhantomJSDriver? Just change driver = new FirefoxDriver()

Actually if I modify the example code provided by selenium, change the firefox drive to phantonjs driver, it works fine. No idea what is the problem using phantomjs.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        System.setProperty("phantomjs.binary.path", System.getProperty("user.dir")+"/phantomjs");
        WebDriver driver = new PhantomJSDriver();
        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());

        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());

        //Close the browser
        driver.quit();
    }
}

I face same problem when i was using phantomjs 1.9.7 version. I think, this following version have some issue. Please used phantomjs 1.9.0 version. This is more stable version.

Here is the download link:https://code.google.com/p/phantomjs/downloads/detail?name=phantomjs-1.9.0-windows.zip&can=4&q=

Kyle B.

Download the most recent selenium java bindings - 2.45. Then remove the 2.44 (or whatever you're using) from your library.

Then include the following in your code:

import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;

public void setUp() throws Exception {

DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe");
WebDriver driver = new PhantomJSDriver(DesireCaps);

NOTE* The location of the .exe may not be in your C drive, so you point it to exactly you've store phantom

You should be good to go after this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!