gpt4 book ai didi

java - PhantomJS GhostDriver XPath 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 15:15:40 24 4
gpt4 key购买 nike

我正在尝试使用 PhantomJS 和 GhostDriver for Java 从 eclipse IDE 运行一个简单的 GoogleSuggest 示例,但是它没有通过 Xpath 找到任何元素,如果我使用其他驱动程序(例如 Firefox),我可以使用相同的 Xpath 表达式找到元素。有人可以让我知道我在这里做错了什么吗? GhostDriver 是否支持 Xpath,我是否缺少 Maven 依赖项中的任何内容?

以下行始终返回空列表。

List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));`

任何帮助将不胜感激!谢谢。

我的简单类(class)

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>

最佳答案

这可能有效。忽略以前的工具 - 它是我们团队构建的更多内部产品,我们正在将其开源。

在此之前您可以探索:http://yslow.org/phantomjs/

关于java - PhantomJS GhostDriver XPath 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com