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

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();
		}
	}
}

  

转载于:https://www.cnblogs.com/sincoolvip/p/7491043.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值