码迷,mamicode.com
首页 > 编程语言 > 详细

selenium+java-查找页面中包含关键字的URL

时间:2017-09-07 19:25:13      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:search   .exe   webdriver   output   oid   写入文件   system   open   list   

package seleniumLearn1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import com.gargoylesoftware.htmlunit.javascript.host.Element;

/**
 * 获取http://www.qyer.com页面中,所有</a>标签"href"属性值
 * 包含英文单词“place”的URL,并将结果保存到“/home/result.log”文件中。
 * @author 0
 *
 */
public class Search {
	
	static String baseUrl="http://www.qyer.com";
	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "E:\\\\webDriver\\\\chromedriverV2.28.exe");
		File logFile = new File("d://logFile.txt");
		if(!(logFile.exists())) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		WebDriver driver = new ChromeDriver();
		driver.get(baseUrl);
		
		//获取所有a标签
		List<WebElement> aList = driver.findElements(By.tagName("a"));
		try {
			Thread.sleep(10000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//便利所有标签
		FileOutputStream fs = null;
		try {
			fs = new FileOutputStream(logFile);
			for (WebElement a : aList) {
				System.out.println(a.getAttribute("href"));//获取a标签中的URL
				
				//获取a标签href属性值
				String urlStr = a.getAttribute("href");
				if(urlStr.contains("place")) {
					urlStr +="\r\n";
					//将URL写入文件中
					fs.write(urlStr.getBytes());
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

  

selenium+java-查找页面中包含关键字的URL

标签:search   .exe   webdriver   output   oid   写入文件   system   open   list   

原文地址:http://www.cnblogs.com/sincoolvip/p/7491043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!