1.webelement ��Դ��
webelement ��Դ��
如果GET方法可以发送请求,那么HttpConnection是可以搞定的,就是短线突破前高指标源码拼接下URL字符串而已。如果是POST方式发送请求的,而且网站只是aws程序源码一个简单提交表单,那么WebDriver这个开源项目,优大麦 源码使用这个项目自带的浏览器驱动(一个简单的浏览器,不会显示浏览器具体操作,但可以模拟相关的操作)是可以满足你的要求的。
给你一个简单的例子:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class TestHtmlUnitDriver {
/
*** @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HtmlUnitDriver dr = new HtmlUnitDriver(false); //使用自动的简单HTML浏览器驱动,false为不显示DEBUG信息。
dr.get("
);
//检查页面title
System.out.println("页面Title:"+dr.getTitle());
WebElement el = dr.findElement(By.xpath("//html"));
// System.out.println(el.getText());
WebElement input = dr.findElement(By.id("kw"));
//搜索关键字
input.sendKeys("webDriver");
WebElement button = dr.findElement(By.id("su"));
//提交表单 webDriver会自动从表单中查找提交按钮并提交
button.click();
//或者直接
//input.submit();
//检查页面title
System.out.println("页面Title:"+dr.getTitle());
dr.close();
}
}
另外一种方式,JDK自带的fm软件源码HttpConnection
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestHttpURLConnection {
/
*** @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url="/s?wd=WebDriver&ie=utf-8";
try {
HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(url).openConnection();
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setUseCaches(true); //使用缓存
httpUrlConnection.connect(); //建立连接
InputStream inputStream = httpUrlConnection.getInputStream(); //读取输入流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String string;
while ((string = bufferedReader.readLine()) != null) {
System.out.println(string); //打印输出
}
bufferedReader.close();
inputStream.close();
httpUrlConnection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //创建连接
}
}