3

私は、Selenium 2 と FireFox ドライバーを使用した (Java/Groovy) ブラウザー自動化アプリの作成に携わっています。

現在、明らかに不正なURI 構文を使用していることが確認された一部の URL に問題があります。(具体的には中括弧 ( {})、|'s、および^'s)。

String url = driver.getCurrentUrl(); // http://example.com/foo?key=val|with^bad{char}acters

java.net.URIaによって返された文字列からa を構築しようとすると、 driver.getCurrentUrl()aURISyntaxExceptionがスローされます。

new URI(url); // java.net.URISyntaxException: Illegal character in query at index ...

urlを構築する前に全体をエンコードURIしても機能しません(私が理解しているように)。

URL全体がエンコードされており、通常の方法で解析できる部分は保持されません. たとえば、この uri-safe 文字列では、クエリ文字列パラメーターの区切り文字としてのa と、単一の qs-param のコンテンツ内の (そのエンコードされた値) URIの違いを認識できません。&%26

String encoded = URLEncoder.encode(url, "UTF-8") // http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval%7Cwith%5E%7Cbad%7Ccharacters
URI uri = new URI(encoded)
URLEncodedUtils.parse(uri, "UTF-8") // []

現在の解決策は、 を構築する前にURI、次の (グルーヴィーな) コードを実行することです。

["|", "^", "{", "}"].each {
    url = url.replace(it, URLEncoder.encode(it, "UTF-8"))
}

しかし、これは汚くて間違っているようです。

私の質問はマルチパートだと思います:

  1. FirefoxDriver が URI ではなく文字列を返すのはなぜですか?
  2. この文字列の形式が正しくないのはなぜですか?
  3. この種のことに対処するためのベストプラクティスは何ですか?
4

4 に答える 4

2

コメントで説明されているように、クエリ文字列パラメーターを部分的にエンコードできますが、機能するはずです。

他の方法は、galimatiasライブラリを使用することです。

import io.mola.galimatias.GalimatiasParseException;
import io.mola.galimatias.URL;

import java.net.URI;
import java.net.URISyntaxException;

public class Main {

    public static void main(String[] args) throws URISyntaxException {
        String example1 = "http://example.com/foo?key=val-with-a-|-in-it";
        String example2 = "http://example.com?foo={bar}";

        try {
            URL url1 = URL.parse(example1);
            URI uri1 = url1.toJavaURI();
            System.out.println(url1);
            System.out.println(uri1);

            URL url2 = URL.parse(example2);
            URI uri2 = url2.toJavaURI();
            System.out.println(url2);
            System.out.println(uri2);
        } catch (GalimatiasParseException ex) {
            // Do something with non-recoverable parsing error
        }
    }
}

出力:

http://example.com/foo?key=val-with-a-|-in-it
http://example.com/foo?key=val-with-a-%7C-in-it
http://example.com/?foo={bar}
http://example.com/?foo=%7Bbar%7D
于 2015-04-08T17:46:58.233 に答える
0

別の解決策は、取得した URL を分割し、それらを使用して必要な URL を作成することです。これにより、URL クラスのすべての機能を確実に取得できます。

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;     
import java.net.URISyntaxException;      
import java.net.URL;

public class Sample {

public static void main(String[] args) throws UnsupportedEncodingException,
        URISyntaxException, MalformedURLException {
    String uri1 = "http://example.com/foo?key=val-with-a-{-in-it";

    String scheme=uri1.split(":")[0];

    String authority=uri1.split("//")[1].split("/")[0];

    String path=uri1.split("//")[1].split("/")[1].split("\\?")[0];  

    String query=uri1.split("\\?")[1];  


    URI uri = null;
    uri = new URI(scheme, authority, "/"+path, query,null);

    URL url = null;

    url = uri.toURL();

    System.out.println("URI's Query:"+uri.getQuery());
    System.out.println("URL's Query:"+url.getQuery());

}

}
于 2015-04-10T09:08:01.663 に答える
0

driver.getCurrentUrl() はブラウザから文字列を取得し、それを URL にする前に、文字列を URL エンコードする必要があります。

Javaでの例については、クエリ文字列パラメーターの Java URL エンコードを参照してください。

于 2015-03-12T18:25:54.640 に答える