zoukankan      html  css  js  c++  java
  • 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 +="
    ";
    					//将URL写入文件中
    					fs.write(urlStr.getBytes());
    				}
    				
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

      

  • 相关阅读:
    如何创建一个自定义和定制Windows窗体事件“跳转列表吗
    动画应用程序的主窗体
    Splasher v1.32 -启动屏幕实现
    颜色需要
    选项卡对话框类
    捕捉屏幕的各种方法
    列表生成方式
    关于日志监控中,关键字段如果还有.该如何处理
    oracle的一些状态查询
    rpm
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/7491043.html
Copyright © 2011-2022 走看看