Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用java填写网站表单,但表单标记嵌入在iframe标记中_Java_Html_Iframe_Jsoup_Httpurlconnection - Fatal编程技术网

尝试使用java填写网站表单,但表单标记嵌入在iframe标记中

尝试使用java填写网站表单,但表单标记嵌入在iframe标记中,java,html,iframe,jsoup,httpurlconnection,Java,Html,Iframe,Jsoup,Httpurlconnection,我的目标是访问此url并使用java填写表单。为此,我尝试查找所有表单标记: import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class HttpUrlConnectionExample{ public static

我的目标是访问此url并使用java填写表单。为此,我尝试查找所有表单标记:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HttpUrlConnectionExample{

    public static void main(String[] args) throws IOException{
        Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
        String page = document.toString();//this is the whole page's html

        Elements formEl = document.getElementsByTag("form");
    }

}
但是,formEl返回空,因为表单标记嵌入在iframe标记(页面源代码片段)中的html中:


我发现您实际上可以创建自己的方法,使用子字符串获取src url,然后使用该字符串获取文档连接:

public static String getSrcString(String html){
    String construct = "";
    for (int i = 0; i < html.length() - 5;i++){
        if (html.substring(i, i + 5).equals("src=\"")){
            i += 5;
            while(!html.substring(i, i + 1).equals("\"")){
                construct += html.substring(i, i + 1);
                i++;
            }
        }
    }
    return construct;
}

我发现您实际上可以创建自己的方法,使用子字符串获取src url,然后使用该字符串获取文档连接:

public static String getSrcString(String html){
    String construct = "";
    for (int i = 0; i < html.length() - 5;i++){
        if (html.substring(i, i + 5).equals("src=\"")){
            i += 5;
            while(!html.substring(i, i + 1).equals("\"")){
                construct += html.substring(i, i + 1);
                i++;
            }
        }
    }
    return construct;
}

不需要使用您自己的
getSrcString
方法,尤其是因为子字符串方法将中断,以便对标记进行最小的更改

在具有
src
属性的元素上使用
.attr(“abs:src”)
(比较:)

示例代码

Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
Element iframeEl = document.select("iframe").first();
String embedURL = iframeEl.attr("abs:src");
Document embedDoc = Jsoup.connect(embedURL).get();

System.out.println(embedDoc.select("form").first());
截断输出

<form action="findit.php" method="post" name="dlrsrchfrm" target="_blank"> 
    <div style="padding: 15px;">
    [...]
</form> 

[...]

不需要您自己的
getSrcString
方法,特别是因为子字符串方法将中断,以便在标记中进行最小的更改

在具有
src
属性的元素上使用
.attr(“abs:src”)
(比较:)

示例代码

Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
Element iframeEl = document.select("iframe").first();
String embedURL = iframeEl.attr("abs:src");
Document embedDoc = Jsoup.connect(embedURL).get();

System.out.println(embedDoc.select("form").first());
截断输出

<form action="findit.php" method="post" name="dlrsrchfrm" target="_blank"> 
    <div style="padding: 15px;">
    [...]
</form> 

[...]