@Arran解決了,硒不能很好地工作,
text()函數,更新xpath到:// table [@width = 200]/tbody/tr [1]/TD/AXPath在Chrome控制檯上工作,但不在硒上
我需要操縱一個內部系統,它永遠不會準時。
我將超時加載和腳本更改爲30小時以查看其真正的時間問題。
我禁用了JavaScript。
沒有工作。
我創建了一個非常小的版本的我的Selenium代碼和一個最小的純HTML文件版本,我把我的網絡服務器。
當我在Chrome上打開並在控制檯上鍵入$ x('// table [@ width = 200]/tbody/tr [1]/td/a/text()')時,xpath得到我想要的。
但是當我在PhantomJSDriver上執行相同的操作時,會有一個neverendig進程(我知道超時設置爲30小時)
當我通過Fiddler比較內容是相同的。
我不需要GPU,但是當我嘗試ChromeDriver時,它拋出一個與gpu相關的exeption:
[4588:4592:0519/143844:錯誤:gpu_info_collector_win.cc(140)]無法從評估結果中讀取遊戲分數。
最小HTML是:
<html>
<head>
<title>Its a test</title>
</head>
<body>
<table width=200>
<tbody>
<tr>
<td>
<a href='http://uej65ge.com/lnk001.html'>text01</a>
</td>
<td>
<a href='http://uej65ge.com/lnk002.html'>text02</a>
</td>
</tr>
<tr>
<td>
<a href='http://uej65ge.com/lnk003.html'>text03</a>
</td>
<td>
<a href='http://uej65ge.com/lnk004.html'>text04</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
的webdriver的代碼是:
// ------------------------- ------------------------
public void start() throws FileNotFoundException {
WebDriver driver;
driver = getPhanthomDriver();
scrapTest(driver);
}
// ----------------- --------------------------------
private WebDriver getPhanthomDriver() {
WebDriver driver = null;
try {
DesiredCapabilities caps = new DesiredCapabilities();
String pathToPhantom = ConfigProvider.getStringConfig("pathToPhantom");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, pathToPhantom);
caps.setJavascriptEnabled(false);
driver = new PhantomJSDriver(caps);
} catch (IOException ex) {
ConsoleHelper.printMessage("ERRO_io:" + ex.getMessage(), new Date());
} catch (Exception ex) {
ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
}
return driver;
}
// --------- -------------- --------------------------
private WebDriver getChromeDriver() {
// [4588:4592:0519/143844:ERROR:gpu_info_collector_win.cc(140)] Could not read gaming score from assessment results.
// I dont need GPU here !!!
WebDriver driver = null;
try {
String pathToChrome = ConfigProvider.getStringConfig("pathToChrome");
System.setProperty("webdriver.chrome.driver", pathToChrome);
driver = new ChromeDriver();
} catch (Exception ex) {
ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
}
return driver;
}
// --------------- ----------------------------------
public void scrapTest(WebDriver driver) throws FileNotFoundException {
// this its form more than 05 minutes without return
try {
long pollingForSecond = 1;
long timeOutInSeconds = 999999;
if(driver == null){ throw new Exception("Erro, driver informado era nulo");}
String initialUrl = "http://devw7lng:8080/CPS/xpts.html";
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.HOURS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.HOURS);
driver.get(initialUrl);
final String xpath_categoriasTexto = "'//table[@width=200]/tbody/tr[1]/td/a/text()'";
List<WebElement> elements = null;
try {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeOutInSeconds, TimeUnit.SECONDS)
.pollingEvery(pollingForSecond, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.ignoring(org.apache.http.NoHttpResponseException.class);
elements = wait.until(new Function<WebDriver, List<WebElement>>() {
@Override
public List<WebElement> apply(WebDriver driver) {
System.out.print(".");
return driver.findElements(By.xpath(xpath_categoriasTexto));
}
});
} catch (Exception err) {
}
boolean ok = false;
ok = ((elements != null) && (elements.size() > 0));
int x = 0;
} catch (Exception ex) {
ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
}
}
// ------- ------------------------------------------
有人在這裏使用selenium的text()函數:http://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing – 2013-05-20 00:20:31