WebDriver getCurrentUrl() 返回格式错误的 URI

WebDriver getCurrentUrl() returning malformed URI

我参与了使用 Selenium 2 和 FireFox 驱动程序编写 (Java/Groovy) 浏览器自动化应用程序。

目前我们在野外发现的一些 URL 存在问题,这些 URL 显然使用了 错误的 URI 语法。 (特别是 curly 大括号 ({})、|^'s)。

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

当尝试从 driver.getCurrentUrl() 编辑的字符串 return 构造 java.net.URI 时,抛出 URISyntaxException

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

Encoding整个url在构造URI之前是行不通的(据我了解)。

整个 url 被编码,它不保留我可以以任何正常方式解析的任何部分。例如,对于这个 uri 安全字符串,URI 无法区分作为查询字符串参数定界符的 & 或内容中的 %26(其编码值)单个 qs 参数。

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、运行之前,使用以下(groovy)代码:

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

但这似乎是错误的。

我想我的问题是多部分的:

  1. 为什么 FirefoxDriver return 是字符串而不是 URI?
  2. 为什么这个字符串格式不正确?
  3. 处理这种事情的最佳实践是什么?

driver.getCurrentUrl() 从浏览器获取一个字符串,在将其变成 URL 之前,您应该 URL 对字符串进行编码。

请参阅 Java URL encoding of query string parameters 以获取 Java 中的示例。

我们可以对查询字符串参数进行部分编码,正如评论中所讨论的那样,它应该可以工作。

其他方法是使用 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

这对你有用吗?

import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;


public class Sample {

public static void main(String[] args) throws UnsupportedEncodingException {
    String urlInString="http://example.com/foo?key=val-with-a-{-in-it";
    String encodedURL=URLEncoder.encode(urlInString, "UTF-8");

    URI encodedURI=URI.create(encodedURL);
    System.out.println("Actual URL:"+urlInString);
    System.out.println("Encoded URL:"+encodedURL);
    System.out.println("Encoded URI:"+encodedURI);

}

}

输出:

Actual URL:http://example.com/foo?key=val-with-a-{-in-it Encoded URL:http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval-with-a-%7B-in-it Encoded URI:http%3A%2F%2Fexample.com%2Ffoo%3Fkey%3Dval-with-a-%7B-in-it

另一个解决方案是拆分获取的 URL,然后使用它们创建您想要的 URL。这将确保您获得 URL class.

的所有功能
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());

}

}