私はここで、xpathセレクターを使用してcasperJSに関するヘルプを取得しようとしている初心者です。
GoogleサイトからXPathを選択したいのですが、入力ボックスを探すのは正確です。HTMLは次のとおりです。
<input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">
<div id="gs_sc0" class="gbqfif" style="background: none repeat scroll 0% 0% transparent; color: transparent; padding: 0px; position: absolute; z-index: 2; white-space: pre; visibility: hidden;"></div>
<input id="gs_taif0" class="gbqfif" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; left: 0px;" dir="ltr">
<input id="gs_htif0" class="gbqfif" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; transition: all 0.218s ease 0s; opacity: 0; left: 0px; text-align: left;" dir="ltr">
ここでの私の単純なコードは機能しません:
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
casper.start('www.google.com');
casper.wait(3000,function(){
this.echo(this.getTitle());
});
casper.then(function(){
casper.sendKeys(x('//*[@id="gbqfq"]'),"newegg.com" );
});
casper.then(function(){
casper.click(x('//*[@id="gbqfq"]'));
});
casper.run();
を使用してスクリプトを実行すると: casperjs search.js
結果は次のようになります:
E:\Projects\casperjs\mytestcase>casperjs search.js
CasperError: Cannot get informations from xpath selector: //*[@id="gbqfq"]: elem
ent not found.
E:/projects/casperjs/modules/casper.js:1058 in getElementInfo
E:/projects/casperjs/modules/casper.js:1589
E:/Projects/casperjs/mytestcase/search.js:21
E:/projects/casperjs/modules/casper.js:1553 in runStep
E:/projects/casperjs/modules/casper.js:399 in checkStep
私は常にxpathから要素を取得することに固執しています。私はcasperJSv1.1.0-beta3と最新のphantomJSバージョンを使用しています。
どんな助けもいただければ幸いです!
問題は、Webリソースではなく、ローカルファイルを指すURLです。Webページのロード後にステータスをダンプすることで、次のことが簡単にわかります。
casper.start('www.google.com', function() {
this.echo(this.status(true));
});
出力には、とりわけ、この行が含まれています(私はからスクリプトを実行しました/tmp
):
"requestUrl": "file:///tmp/www.google.com",
この問題を解決するには、次を追加しますhttp://
(またはhttps://
暗号化を希望する場合)。
casper.start('http://www.google.com');
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加