S-IDE では操作対象の要素を xpath で指定することができる。
例:xpathの明示
コマンド | 対象 | 値 |
click | xpath=//h2/a | |
対象要素を特定するこの機能は Element Locator と呼ばれ、
Selenium Core で提供されている。
1 BrowserBot.prototype.findElement = function(locator, win) {
2 var element = this.findElementOrNull(locator, win);
3 if (element == null) throw new SeleniumError("Element " + locator + " not found");
4 return element;
5 }
(selenium-c
ore-1.0.1/
core/scrip
ts/seleniu
m-browserb
ot.js)
従って、S-IDE の対象指定以外での場所、
例えば自作関数内や getEval の中などで使いたい場合には、
上記の findElement を直接呼び出すとよい。
1 function isHpFull(percent) {
2 var path = '//tr[0]/td[1]';
3 var hp = selenium.browserbot.findElement(path).split('/');
4 var hp1 = parseInt(hp[0]);
5 var hp2 = parseInt(hp[1]);
6 return ((hp1 * 100 / hp2) > percent);
7 }
例:HPが90%以上ある場合は宿屋に入らない
コマンド | 対象 | 値 |
gotoIf | isHpFull(90) | skip_inn |
click | label=宿屋 | |
label | skip_inn | |
関連として、Selenium 環境ではない状態の Mozilla で xpath を扱うには、
document.evaluate が便利である。
1 function xpath2text(path) {
2 var result = document.evaluate(path, doc, null, 7, null);
3 var item = result.snapshotItem(0);
4 return item.innerHTML;
5 }
参考
- https://developer.mozilla.org/ja/Introduction_to_using_XPath_in_JavaScript