无法使用PhantomJS找到Element但与Firefox Selenium Java

时间:2016-05-19 10:02:44

标签: java selenium dom xpath phantomjs

我正在使用 Selenium PhantomJS WebDriver 来抓取 DOM 元素。

尝试:

driver.findElementByXPath("//*[@class=\"_XWk\"]").getText()

适用于 Firefox WebDriver ,但 适用于 PhantomJS WebDriver

显示错误

引起:org.openqa.selenium.NoSuchElementException:错误消息=> '无法找到带有xpath的元素'// * [@ class =“_ XWk”]''

以下是我尝试的示例代码

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestPhantomJs {
    public static void testSearchReturnsResults() throws Exception {
        // Create instance of PhantomJS driver
        DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
        PhantomJSDriver driver = new PhantomJSDriver(capabilities);
//      DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//      FirefoxDriver driver = new FirefoxDriver(capabilities);

        // Navigate to the page
        driver.get("http://www.google.co.in");
        driver.manage().window().setSize(new Dimension(1124, 850));
        System.out.println(driver.getTitle());

        // Input the search term into the search box
        String searchTerm = "Prime Minister of India";
        WebElement element = driver.findElement(By.cssSelector("input[name='q']"));
        element.sendKeys(searchTerm);
        element.sendKeys(Keys.RETURN);

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        System.out.println(driver.findElementByXPath("//*[@class=\"_XWk\"]").getText());
        System.out.println("\nFinish...");
        driver.close();
    }

    public static void main(String[] args) throws Exception {
        testSearchReturnsResults();
    }
}

0 个答案:

没有答案