我正在尝试使用PhantomJS和GhostDriver for Java从eclipse IDE运行一个简单的GoogleSuggest示例,但它没有找到Xpath的任何元素,如果我使用像Firefox这样的其他驱动程序我能够使用相同的Xpath表达式查找元素。有人可以让我知道我在做错了吗? GhostDriver是否支持Xpath,我在maven dependecies中遗漏了什么?
以下行始终返回空列表。
List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));`
任何帮助将不胜感激!感谢。
我的简单课程
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class GoogleSuggest {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
//WebDriver driver = new PhantomJSDriver();
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/usr/local/bin/phantomjs");
PhantomJSDriver driver = new PhantomJSDriver(capabilities);
// Go to the Google Suggest home page
driver.get("http://www.google.com/webhp?complete=1&hl=en");
// Enter the query string "Cheese"
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("Cheese");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
// And now list the suggestions
List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));
for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
}
Eclipse控制台输出:
Jan 01, 2014 2:37:14 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: /usr/local/bin/phantomjs
Jan 01, 2014 2:37:14 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 23886
Jan 01, 2014 2:37:14 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=23886, --webdriver-logfile=/home/general/workspace/TestSample/phantomjsdriver.log]
Jan 01, 2014 2:37:14 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
PhantomJS is launching GhostDriver...
[INFO - 2014-01-01T19:37:14.707Z] GhostDriver - Main - running on port 23886
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO - 2014-01-01T19:37:15.345Z] Session [1e2756e0-731c-11e3-b767-e95df6be38fb] - _decorateNewWindow - page.settings: {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34","webSecurityEnabled":true}
[INFO - 2014-01-01T19:37:15.345Z] Session [1e2756e0-731c-11e3-b767-e95df6be38fb] - page.customHeaders: - {}
[INFO - 2014-01-01T19:37:15.345Z] Session [1e2756e0-731c-11e3-b767-e95df6be38fb] - CONSTRUCTOR - Desired Capabilities: {"platform":"ANY","browserName":"phantomjs","phantomjs.binary.path":"/usr/local/bin/phantomjs","version":""}
[INFO - 2014-01-01T19:37:15.345Z] Session [1e2756e0-731c-11e3-b767-e95df6be38fb] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.2","driverName":"ghostdriver","driverVersion":"1.0.4","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO - 2014-01-01T19:37:15.346Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 1e2756e0-731c-11e3-b767-e95df6be38fb
[INFO - 2014-01-01T19:42:14.705Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW
[INFO - 2014-01-01T19:47:14.754Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW
我的Maven参赛作品:
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version>
</dependency>